<?php
/**
 * 产品管理
 * @author cjli
 *
 */
class ProductAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	}

	public function all()
	{
		global $language_id;
		$cat_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		$order = isset($_GET['order']) ? $_GET['order'] : null;
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : null;
		$orderby = isset($_GET['oby']) ? $_GET['oby'] : 'DESC';
		$sale=isset($_GET['sale'])?intval($_GET['sale']):0;
		if(!in_array($orderby, array('ASC','DESC'))){
			$orderby = 'DESC';
		}
		/*
		$where = array(
			'is_sell' => 1,
			'new_product' => 1,
		);
		*/
		$where['is_sell'] =1;
		$where['is_translate'] =0;
		if(!$keyword&&!$sale){
			$where['new_product'] =1;
		}
		$style = $_REQUEST['style'];
		$colour = $_REQUEST['colour'];
		if($style){
			$where['style'] = $style;
		}

		if($colour){
			$where['colour'] = $colour;
		}
		if($keyword){
			$where['keyword'] = $keyword;
		}
		if($cat_id){
			$where['catid'] = $cat_id;
			$categoryInfo = CategoryModel::get_category_info($cat_id);
			$data['categoryInfo'] = $categoryInfo;
		}
		$order = $order ? $order : 1;
		if(in_array($order, array('1','2','3')))
		{
			if($order == '1') $where['orderby'] = 'product.id '. $orderby;
			if($order == '2') $where['orderby'] = 'product.sell_price '. $orderby;
			elseif($order == '3') $where['orderby'] = 'product.end_time '. $orderby;
		}else{

		}
		$data['orderby'] = $order;
		$data['oby'] = $orderby;
		$where['orderby'] = 'new_product desc,product.sorts asc, product.id DESC ';//默认进入商品列表 价格从低到高排序

		//分页显示数
		$limit_number = C('product_list_num');
		//产品总数
		$productList = array();
		$productCount = 0;
		$productCount = ProductModel::get_product_count($where);
		if($productCount){
			require THEMES_PATH.'page.php';// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
		}
		//dump($productList);exit;
		$data['productCount'] = $productCount;
		$data['productList'] = $productList;
        if($cat_id){
            $this->assign('cat_ids',$cat_id);//当前分类Id
        }else{
            $this->assign('cat_ids',0);//当前分类Id
        }
		//营销代码，NEW页面分类标题以及关键词
		$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
		if(preg_match('/^\/new\/2093-[a-z]+-[a-z]+-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2134;
		}elseif(preg_match('/^\/new\/2061-[a-z]+-[a-z]+-[a-z]+$/', $the_url)){
            $seo_categoryid = 2133;
		}elseif(preg_match('/^\/new\/2104-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2135;
		}elseif(preg_match('/^\/new\/2062-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2136;
		}elseif(preg_match('/^\/new\/2066-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2137;
		}elseif(preg_match('/^\/new\/2103-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2138;
		}elseif(preg_match('/^\/new\/2102-[a-z]+$/', $the_url)){
	        $seo_categoryid = 2139;
		}elseif(preg_match('/^\/new\/2067-[a-z]+$/', $the_url)){
		    $seo_categoryid = 2140;
		}else{
			$seo_categoryid = 2141;
		}
        if($seo_categoryid){
	        $seo_category = CategoryModel::get_category_info($seo_categoryid);
			$data['docTitle']       = $seo_category['meta_title'] ? $seo_category['meta_title'] : $seo_category['name'];
			$data['docKeywords']    = $seo_category['meta_keywords'];
			$data['docDescription'] = $seo_category['meta_description'];
        }

        $this->assign($data);

		$style_array = D('Category')->getCategoryList(8);
		$this->assign('style_array', $style_array);


		$style_colour = D('Category')->getCategoryList(9);
		$this->assign('style_colour', $style_colour);

		//类型属性列表
		if($style){
			$style02 = explode('_', $style);
			$stylename = '';
			foreach ($style02 as $styleId){
				if($stylename == ''){
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename = $stylegoryInfo['name'];
				}else{
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename .= ','.$stylegoryInfo['name'];
				}
			}
		}
		//列表颜色
			if($colour){
			//dump($colour);
			$colour02 = explode('_', $colour);
			$colourname = '';
			foreach ($colour02 as $colourId){
				if($colourname == ''){
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname = $colourgoryInfo['name'];
				}else{
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname .= ','.$colourgoryInfo['name'];
				}
			}
		}

		//搜索条件
		unset($where['orderby']);
		$styleids = explode('_', $style);
		$this->assign('styleids', $styleids);
		$this->assign('style_ids', $style);
		$this->assign('stylename', $stylename);

		$colourids = explode('_', $colour);
		$this->assign('colourids', $colourids);
		$this->assign('colour_ids', $colour);

		$this->assign('urlArray', $where); //搜索条件

		$this->assign('colourname',$colourname);
		$this->assign('cat_id',$cat_id);
        if(check_wap()||$_GET['mobile']==1){
            $this->assign('mobile',1);
        }
        $parent_category=CategoryModel::get_category_sub_list(0);
        foreach($parent_category as $k=>$v){
            if(!$v['is_3d']){
                unset($parent_category[$k]);
            }
        }
        //============属性下的商品start
        if(cache('category_procolor_new'.$cat_id.$keyword)){
            $category_procolor=cache('category_procolor_new'.$cat_id.$keyword);
        }else{
            $category_procolor = CategoryModel::getCategoryList(11);
            foreach ($category_procolor as $k=>$v){
                $where2=" FIND_IN_SET(".$v['id'].",product_colour_id)";
                $category_procolor[$k]['pro_count'] = ProductModel::get_product_count($where,$where2);//echo D("product")->getLastsql();exit;
                if($category_procolor[$k]['pro_count']==0){
                    unset($category_procolor[$k]);
                }
            }
            cache('category_procolor_new'.$cat_id.$keyword,$category_procolor,30*60);
        }
        if(cache('category_color_new'.$cat_id.$keyword)){
            $category_color=cache('category_color_new'.$cat_id.$keyword);
        }else{
            $category_color= CategoryModel::getCategoryList(9);
            foreach ($category_color as $k=>$v){
                $where2=" FIND_IN_SET(".$v['id'].",colour_id) ";
                $category_color[$k]['pro_count'] = ProductModel::get_product_count($where,$where2);
                if($category_color[$k]['pro_count']==0){
                    unset($category_color[$k]);
                }
            }
            cache('category_color_new'.$cat_id.$keyword,$category_color,30*60);
        }
        //print_r($category_procolor);
        //============属性下的商品over
        
        $this->assign('category_procolor', $category_procolor);
        $this->assign('category_color', $category_color);
        $this->assign('parent_category',$parent_category);
        $this->assign('is_new',1);
		$this->display('product_all');
	}

	//列表页
	public function category()
	{
		//send_http_status(301);
		//redirect('product/rewrite_category/$product_items_info["name"]');
		global $language_id;
		$cat_id = isset($_GET['cid']) ? intval($_GET['cid']): null;
		if($cat_id){
			redirect(url('/category/cids/'.$cat_id));
		}
		$cat_ids = $_REQUEST['cids'];
		if($cat_ids || !$_REQUEST){
			send_http_status(404);
			$this->display(C('ERROR_PAGE'));
	    	exit;
		}
		$recommend_id = isset($_GET['rid']) ? intval($_GET['rid']) : null;
		$brand_id = isset($_GET['bid']) ? intval($_GET['bid']) : null;
		$order = isset($_GET['order']) ? intval($_GET['order']) : null;
		$filter_attr = isset($_GET['attr']) ? $_GET['attr'] : null;
		$price_where = isset($_GET['price']) ? $_GET['price'] : null;
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : null;
		$orderby = isset($_GET['oby']) ? $_GET['oby'] : 'ASC';
		$style = $_REQUEST['style'];
		$colour = $_REQUEST['colour'];
		/*if(!strstr($cat_ids, '_',$style,'_')){
			send_http_status(301);
		redirect(url('/category/2061-SUITS'));
		exit;
		}*/
		if($order || $price_where || $style || $colour){
	    	send_http_status(404);
			$this->display(C('ERROR_PAGE'));
	    	exit;
		}
		if(!in_array($orderby, array('ASC','DESC'))){
			$orderby = 'ASC';
		}

		$where = array(
			'is_sell' => 1,
		);

		if($keyword){
			$where['keyword'] = $keyword;
		}
		$where['orderby'] = 'advanced_price '.$orderby;//默认进入商品列表 价格从低到高排序
		if(in_array($order, array('1','2','3')))
		{
			if($order == '1') $where['orderby'] = 'id '.$orderby;
			if($order == '2') $where['orderby'] = 'advanced_price '.$orderby;
			elseif($order == '3') $where['orderby'] = 'end_time '.$orderby;
		}

		$data['orderby'] = $order ? $order : '1';
		$data['oby'] = $orderby;

		//推荐分类
		if($recommend_id){
			$where['rid'] = $recommend_id;
			$recommendInfo = CategoryModel::get_category_info($recommend_id, CategoryModel::TYPE_RECOMMEND);
			$docTitle = $recommendInfo['meta_title'] ? $recommendInfo['meta_title'] : $recommendInfo['name'];
			$docKeywords = $recommendInfo['docKeywords'];
			$docDescription = $recommendInfo['docDescription'];
			$data['recommendInfo'] = $recommendInfo;
		}

		//品牌
		if($brand_id){
			$where['bid'] = $brand_id;
			$brandInfo = CategoryModel::get_category_info($brand_id, CategoryModel::TYPE_BRAND);
			$docTitle = $brandInfo['meta_title'] ? $brandInfo['meta_title'] : $brandInfo['name'];
			$docKeywords = $brandInfo['docKeywords'];
			$docDescription = $brandInfo['docDescription'];
			$data['brandInfo'] = $brandInfo;
		}


		//产品分类
		if($cat_ids && !strstr($cat_ids, '_')){
			$searchCategoryInfo = $categoryInfo = CategoryModel::get_category_info($cat_ids);
			$model_id = $categoryInfo['model_id'];
			$docTitle = $categoryInfo['meta_title'] ? $categoryInfo['meta_title'] : $categoryInfo['name'];
			$docKeywords = $categoryInfo['meta_keywords'];
			$docDescription = $categoryInfo['meta_description'];
			$data['categoryInfo'] = $categoryInfo;
		}elseif($cat_id){
			$where['catid'] = $cat_id;
			$searchCategoryInfo = $categoryInfo = CategoryModel::get_category_info($cat_ids);
			$model_id = $categoryInfo['model_id'];
			$docTitle = $categoryInfo['meta_title'] ? $categoryInfo['meta_title'] : $categoryInfo['name'];
			$docKeywords = $categoryInfo['meta_keywords'];
			$docDescription = $categoryInfo['meta_description'];
		}
		//类型属性列表
		if($style){
			//dump($style);
			$style02 = explode('_', $style);
			$stylename = '';
			foreach ($style02 as $styleId){
				if($stylename == ''){
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename = $stylegoryInfo['name'];
				}else{
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename .= ','.$stylegoryInfo['name'];
				}
			}
		}

		if($colour){
			//dump($colour);
			$colour02 = explode('_', $colour);
			$colourname = '';
			foreach ($colour02 as $colourId){
				if($colourname == ''){
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname = $colourgoryInfo['name'];
				}else{
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname .= ','.$colourgoryInfo['name'];
				}
			}
		}

		$cat_ids02 = $cat_ids;
		if($cat_ids02){
			$category_db = D('category');
			$catids02 = explode('_', $cat_ids02);
			$cids_value = '';
			$categoryname = '';
			foreach ($catids02 as $cids){
				if($cids_value == ''){
					$cids_value = $cids;
				}else{
					$cids_value .= ','.$cids;
				}

				if($categoryname == ''){
					$categoryInfo02 = CategoryModel::get_category_info($cids);
					$categoryname = $categoryInfo02['name'];
				}else{
					$categoryInfo02 = CategoryModel::get_category_info($cids);
					$categoryname .= ','.$categoryInfo02['name'];
				}
			}

			$where['id'] = array('IN', $cids_value);
			$category_db->field('category_id,colour_id');
			$category_db->where($where);
			$categoryList = $category_db->select();

			foreach ($categoryList as $cate_info){
				$cat_ids02 = explode(',', $cate_info['category_id']);
				$colour_ids = explode(',', $cate_info['colour_id']);

				foreach ($cat_ids02 as $ca_id){
					$category_items_db = D('category_items');
					$category_items_db->field('name');
					$categoryInfo = $category_items_db->where(array('category_id' => $ca_id, 'language_id' => $language_id))->find();
					if(!$categoryInfo){
						$categoryInfo = $category_items_db->where(array('category_id' => $ca_id, 'language_id' => C('language_id')))->find();
					}
					$cat_name[$ca_id]['name'] = $categoryInfo['name'];
					$cat_name[$ca_id]['id'] = $ca_id;
				}
				foreach ($colour_ids as $co_id){
					$category_items_db = D('category_items');
					$category_items_db->field('name');
					$categoryInfo = $category_items_db->where(array('category_id' => $co_id, 'language_id' => $language_id))->find();
					if(!$categoryInfo){
						$categoryInfo = $category_items_db->where(array('category_id' => $co_id, 'language_id' => C('language_id')))->find();
					}
					$colour_name[$co_id]['name'] = $categoryInfo['name'];
					$colour_name[$co_id]['id'] = $co_id;
				}
			}
		}
		$categoryInfo02['adv_img']=$categoryInfo02['adv_img']?$categoryInfo02['adv_img']:'/data/category/default_banner.jpg';//分类banner
		$this->assign('categoryImg',$categoryInfo02['adv_img']);
		$this->assign('categoryname', $categoryname);
		$this->assign('stylename', $stylename);
		$this->assign('colourname', $colourname);
		$this->assign('cat_name', $cat_name);
		$this->assign('colour_name', $colour_name);

		//分页显示数
		$limit_number = C('product_list_num');

		//价格
		if($price_where){
			$where['price'] = $price_where;
		}

		if($cat_ids){
			$where['catids'] = $cat_ids;
		}

		if($style){
			$where['style'] = $style;
		}

		if($colour){
			$where['colour'] = $colour;
		}

		//产品总数
		$productList = array();
		$productCount = 0;
		$productCount = ProductModel::get_product_count($where);
		if($productCount){
			require THEMES_PATH.'page.php';// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
//		}else{
//			$productList = ProductModel::get_product_list(array(), 1, $limit_number);
//			$productCount = count($productList);
		}
		//dump($productList);exit;

		$productNumber = 0;
		$productNumber = OrderModel::get_product_count();//获取这周的销售数量
		for($i=4;$i>=0;$i--){
			if(substr($productNumber,$i,1) > 0)
				$productSum[] = substr($productNumber,$i,1);
			else
				$productSum[] = 0;

		}
		$data['productSum'] = $productSum;
		$data['productCount'] = $productCount;
		$data['productList'] = $productList;

		$data['docTitle'] = $docTitle;
		$data['docKeywords'] = $docKeywords;
		$data['docDescription'] = $docDescription;

		//当前路径
		$pathArray = array();
		if(isset($categoryInfo) && $categoryInfo){
			$pathArray = CategoryModel::get_category_pos($categoryInfo['id']);
		}

		$catids = explode('_', $cat_ids);
		$this->assign('catids', $catids);
		$this->assign('cat_ids', $cat_ids);

		$styleids = explode('_', $style);
		$this->assign('styleids', $styleids);
		$this->assign('style_ids', $style);

		$colourids = explode('_', $colour);
		$this->assign('colourids', $colourids);$colourids = explode('_', $colour);
		$this->assign('colourids', $colourids);
		$this->assign('colour_ids', $colour);
		$this->assign('colour_ids', $colour);

		$this->assign('pathArray', $pathArray);
		unset($where['orderby']);
		$this->assign('urlArray', $where); //搜索条件
		$this->assign($data);
		$this->assign('orderbys', $orderby);
		$this->display('productlist');
	}

        //url重定向
	public function rewrite_category()
	{
		global $language_id;
		$cat_id = isset($_GET['cid']) ? intval($_GET['cid']): null;
		if($cat_id){
			redirect(url('/category/cids/'.$cat_id));
		}
		$cat_ids = $_REQUEST['cids'];
        //对url地址过滤
        //省略url中问号后面的参数
        $wenhao_index = strpos($_SERVER['REQUEST_URI'],'?');
        if($wenhao_index){
         $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'],0,$wenhao_index);
        }
        if($cat_ids){
            $pathinfo = strtolower($_SERVER['REQUEST_URI']);
            $category_pathinfo =  S('category_pathinfo');

            if(!isset($category_pathinfo[$cat_ids])){
                $category_list_base = CategoryModel::get_category_list_base(1, NULL);
                $category_pathinfo = array();
                foreach ($category_list_base as $key => $value) {
                    if($value['parentid'] != 2132)  //2132隐藏
                        $category_pathinfo[$value['id']] = $value['URL_PATH_INFO'];
                }
                if(!isset($category_pathinfo[$cat_ids])){
                    send_http_status(404);
                    $this->display(C('ERROR_PAGE'));
                    exit;
                }
                S('category_pathinfo',$category_pathinfo);

            }

            if(strtolower($category_pathinfo[$cat_ids])!= strtolower($pathinfo) ){
                send_http_status(301);
                redirect(url($category_pathinfo[$cat_ids]));
                exit;
            }
        }


		$recommend_id = isset($_GET['rid']) ? intval($_GET['rid']) : null;
		$brand_id = isset($_GET['bid']) ? intval($_GET['bid']) : null;
		$order = isset($_GET['order']) ? intval($_GET['order']) : null;
		$filter_attr = isset($_GET['attr']) ? $_GET['attr'] : null;
		$price_where = isset($_GET['price']) ? $_GET['price'] : null;
		$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : null;
		$orderby = isset($_GET['oby']) ? $_GET['oby'] : 'ASC';
		$style = $_REQUEST['style'];
		$colour = $_REQUEST['colour'];

		//将URL大写跳转为小写的URl
		$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
		if(preg_match('/^\/[a-z]+\/[0-9]+-[A-Z]+$/', $the_url)){
			send_http_status(301);
			redirect(url('/category/2062-shirts'));
			exit;
		}elseif(preg_match('/^\/[a-z]+\/[0-9]+-[A-Z]+-[A-Z]+$/', $the_url)) {
			send_http_status(301);
			redirect(url('/category/2098-pocket-square'));
			exit;
		}

		if(!in_array($orderby, array('ASC','DESC'))){
			$orderby = 'ASC';
		}

		$where = array(
			'is_sell' => 1,
		);

		if($keyword){
			$where['keyword'] = $keyword;
		}
        $sub_category_sort = array(//如果分类下面有高级和普通，先显示普通分类,再显示高级分类
            2093 => array(2127,2119),
            2061 => array(2123,2120),
            2062 => array(2125,2126),
            2104 => array(2128,2129),
        );
		$where['orderby'] = 'sorts asc, advanced_price '.$orderby . ', catid '.$orderby;//默认进入商品列表 价格从低到高排序
        foreach($sub_category_sort as $k=>$v){
            if($cat_ids == $k){
                if($v[0] > $v[1]){
                   $orderby1 = ' DESC ';
                }else{
                    $orderby1 = ' ASC ';
                }
                 $where['orderby'] =   ' new_product desc,product.sorts asc, product.id DESC';//默认进入商品列表 价格从低到高排序
            }
        }

        if(in_array($order, array('1','2','3')))
		{
			if($order == '1') $where['orderby'] = 'id '.$orderby;
			if($order == '2') $where['orderby'] = 'advanced_price '.$orderby;
			elseif($order == '3') $where['orderby'] = 'end_time '.$orderby;
		}

		$data['orderby'] = $order ? $order : '1';
		$data['oby'] = $orderby;

		//推荐分类
		if($recommend_id){
			$where['rid'] = $recommend_id;
			$recommendInfo = CategoryModel::get_category_info($recommend_id, CategoryModel::TYPE_RECOMMEND);
			$docTitle = $recommendInfo['meta_title'] ? $recommendInfo['meta_title'] : $recommendInfo['name'];
			$docKeywords = $recommendInfo['docKeywords'];
			$docDescription = $recommendInfo['docDescription'];
			$data['recommendInfo'] = $recommendInfo;
		}

		//品牌
		if($brand_id){
			$where['bid'] = $brand_id;
			$brandInfo = CategoryModel::get_category_info($brand_id, CategoryModel::TYPE_BRAND);
			$docTitle = $brandInfo['meta_title'] ? $brandInfo['meta_title'] : $brandInfo['name'];
			$docKeywords = $brandInfo['docKeywords'];
			$docDescription = $brandInfo['docDescription'];
			$data['brandInfo'] = $brandInfo;
		}


		//产品分类
		if($cat_ids && !strstr($cat_ids, '_')){
			$searchCategoryInfo = $categoryInfo = CategoryModel::get_category_info($cat_ids);
			$model_id = $categoryInfo['model_id'];
			$docTitle = $categoryInfo['meta_title'] ? $categoryInfo['meta_title'] : $categoryInfo['name'];
			$docKeywords = $categoryInfo['meta_keywords'];
			$docDescription = $categoryInfo['meta_description'];
			$data['categoryInfo'] = $categoryInfo;
		}elseif($cat_id){
			$where['catid'] = $cat_id;
			$searchCategoryInfo = $categoryInfo = CategoryModel::get_category_info($cat_ids);
			$model_id = $categoryInfo['model_id'];
			$docTitle = $categoryInfo['meta_title'] ? $categoryInfo['meta_title'] : $categoryInfo['name'];
			$docKeywords = $categoryInfo['meta_keywords'];
			$docDescription = $categoryInfo['meta_description'];
		}
		//类型属性列表
		if($style){
			//dump($style);
			$style02 = explode('_', $style);
			$stylename = '';
			foreach ($style02 as $styleId){
				if($stylename == ''){
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename = $stylegoryInfo['name'];
				}else{
					$stylegoryInfo = CategoryModel::get_category_info($styleId,CategoryModel::TYPE_STYLE);
					$stylename .= ','.$stylegoryInfo['name'];
				}
			}
		}

		if($colour){
			//dump($colour);
			$colour02 = explode('_', $colour);
			$colourname = '';
			foreach ($colour02 as $colourId){
				if($colourname == ''){
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname = $colourgoryInfo['name'];
				}else{
					$colourgoryInfo = CategoryModel::get_category_info($colourId,CategoryModel::TYPE_colour);
					$colourname .= ','.$colourgoryInfo['name'];
				}
			}
		}

		$cat_ids02 = $cat_ids;
		if($cat_ids02){
			$category_db = D('category');
			$catids02 = explode('_', $cat_ids02);
			$cids_value = '';
			$categoryname = '';
			foreach ($catids02 as $cids){
				if($cids_value == ''){
					$cids_value = $cids;
				}else{
					$cids_value .= ','.$cids;
				}

				if($categoryname == ''){
					$categoryInfo02 = CategoryModel::get_category_info($cids);
                   // if(isset($categoryInfo02['parent'])))
                    if(isset($categoryInfo02['parent_name'])){
                        $categoryname = $categoryInfo02['parent_name'];
                    }else{
                        $categoryname = $categoryInfo02['name'];
                    }
                    $category_self_name = $categoryInfo02['name'];
                    $this->assign('self_name',$category_self_name);
				}else{
					$categoryInfo02 = CategoryModel::get_category_info($cids);
					$categoryname .= ','.$categoryInfo02['name'];
				}
			}

			$where['id'] = array('IN', $cids_value);
			$category_db->field('category_id,colour_id');
			$category_db->where($where);
			$categoryList = $category_db->select();

			foreach ($categoryList as $cate_info){
				$cat_ids02 = explode(',', $cate_info['category_id']);
				$colour_ids = explode(',', $cate_info['colour_id']);

				foreach ($cat_ids02 as $ca_id){
					$category_items_db = D('category_items');
					$category_items_db->field('name');
					$categoryInfo = $category_items_db->where(array('category_id' => $ca_id, 'language_id' => $language_id))->find();
					if(!$categoryInfo){
						$categoryInfo = $category_items_db->where(array('category_id' => $ca_id, 'language_id' => C('language_id')))->find();
					}
					$cat_name[$ca_id]['name'] = $categoryInfo['name'];
					$cat_name[$ca_id]['id'] = $ca_id;
				}
				foreach ($colour_ids as $co_id){
					$category_items_db = D('category_items');
					$category_items_db->field('name');
					$categoryInfo = $category_items_db->where(array('category_id' => $co_id, 'language_id' => $language_id))->find();
					if(!$categoryInfo){
						$categoryInfo = $category_items_db->where(array('category_id' => $co_id, 'language_id' => C('language_id')))->find();
					}
					$colour_name[$co_id]['name'] = $categoryInfo['name'];
					$colour_name[$co_id]['id'] = $co_id;
				}
			}
		}
		$categoryInfo02['adv_img']=$categoryInfo02['adv_img']?$categoryInfo02['adv_img']:'/data/category/default_banner.jpg';//分类banner
		$this->assign('categoryImg',$categoryInfo02['adv_img']);
		$this->assign('categoryname', $categoryname);
		$this->assign('stylename', $stylename);
		$this->assign('colourname', $colourname);
		//$this->assign('cat_name', $cat_name);
		$this->assign('colour_name', $colour_name);

		//分页显示数
		$limit_number = C('product_list_num');

		//价格
		if($price_where){
			$where['price'] = $price_where;
		}

		if($cat_ids){
			$where['catids'] = $cat_ids;
		}

		if($style){
			$where['style'] = $style;
		}

		if($colour){
			$where['colour'] = $colour;
		}

		//产品总数
		$productList = array();
		$productCount = 0;
		$productCount = ProductModel::get_product_count($where);
		if($productCount){
			require THEMES_PATH.'page.php';// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
//		}else{
//			$productList = ProductModel::get_product_list(array(), 1, $limit_number);
//			$productCount = count($productList);
		}
		//dump($productList);exit;

		$productNumber = 0;
		$productNumber = OrderModel::get_product_count();//获取这周的销售数量
		for($i=4;$i>=0;$i--){
			if(substr($productNumber,$i,1) > 0)
				$productSum[] = substr($productNumber,$i,1);
			else
				$productSum[] = 0;

		}
		$data['productSum'] = $productSum;
		$data['productCount'] = $productCount;
		$data['productList'] = $productList;

		$data['docTitle'] = $docTitle;
		$data['docKeywords'] = $docKeywords;
		$data['docDescription'] = $docDescription;

		//面料名称，价格，说明
		$product_db = D('product');
		$product_db->join('product_items ON product_items.product_id = product.id');
		$fabricinfo = $product_db->where(array('model_id'=>19,'language_id'=>$language_id))->find();
		$this->assign('fabricinfo',$fabricinfo);


		//当前路径
		$pathArray = array();
		if(isset($categoryInfo) && $categoryInfo){
			$pathArray = CategoryModel::get_category_pos($categoryInfo['id']);
		}
        //列表页导航显示调整
        $has_parent_id = $category_db->field('id,parentid')->where('id ='.$cat_ids.' and display=1')->find();
        if($has_parent_id['parentid'] != 0){
            $this->assign('has_parent_id',$has_parent_id);
        }
		$catids = explode('_', $cat_ids);
		$this->assign('catids', $catids);
		$this->assign('cat_ids', $cat_ids);

		$styleids = explode('_', $style);
		$this->assign('styleids', $styleids);
		$this->assign('style_ids', $style);

		$colourids = explode('_', $colour);
		$this->assign('colourids', $colourids);$colourids = explode('_', $colour);
		$this->assign('colourids', $colourids);
		$this->assign('colour_ids', $colour);
		$this->assign('colour_ids', $colour);

		$this->assign('pathArray', $pathArray);
		unset($where['orderby']);
		$this->assign('urlArray', $where); //搜索条件
		$this->assign($data);
		$this->assign('orderbys', $orderby);

		if($cat_name){
			foreach($cat_name as $row){
				if(in_array($row['id'],$styleids)){
					echo "i d ";exit;
				}
			}
		}
		$parent_category=CategoryModel::get_category_sub_list(0);
		foreach($parent_category as $k=>$v){
		    if(!$v['is_3d']){
		        unset($parent_category[$k]);
		    }
		}
		$this->assign('parent_category',$parent_category);
        if(check_wap()||$_GET['mobile']==1){
            $this->assign('mobile',1);
        }
        //============属性下的商品start
        if(cache('category_procolor'.$cat_ids)){
            $category_procolor=cache('category_procolor'.$cat_ids);
        }else{
            $category_procolor = CategoryModel::getCategoryList(11);
            foreach ($category_procolor as $k=>$v){
                $where2=" FIND_IN_SET(".$v['id'].",product_colour_id)";
                 
                $category_procolor[$k]['pro_count'] = ProductModel::get_product_count($where,$where2);
                if($category_procolor[$k]['pro_count']==0){
                    unset($category_procolor[$k]);
                }
            }
            cache('category_procolor'.$cat_ids,$category_procolor,1*60*60);
        }
        if(cache('category_color'.$cat_ids)){
            $category_color=cache('category_color'.$cat_ids);
        }else{
            $category_color= CategoryModel::getCategoryList(9);
            foreach ($category_color as $k=>$v){
                $where2=" FIND_IN_SET(".$v['id'].",colour_id)";
                $category_color[$k]['pro_count'] = ProductModel::get_product_count($where,$where2);
                if($category_color[$k]['pro_count']==0){
                    unset($category_color[$k]);
                }
            }
            cache('category_color'.$cat_ids,$category_color,1*60*60);
        }
        //print_r($category_procolor);
        //============属性下的商品over
        
        $this->assign('category_procolor', $category_procolor);
        $this->assign('category_color', $category_color);
        $this->assign('is_new',0);
        $this->assign('cat_ids', $cat_ids);//当前分类Id
		$this->display('productlist');
	}
    //移动端试衣间
    public function product_mobile()
    {
        $parent_category=CategoryModel::get_category_sub_list(0);
        foreach($parent_category as $k=>$v){
            if(!$v['is_3d']){
                unset($parent_category[$k]);
            }
        }
        $this->assign('parent_category',$parent_category);
        $this->display('product_mobile');
    }
	//产品详细页
	public function info()
	{

		global $language_id;
		$pid = intval($_GET['id']);
		if(!$pid){
			redirect(url(''));
		}

		//产品信息
		$product = ProductModel::get_product_info($pid);
		//url 301重定向
		send_http_status(301);
		redirect(url('/product/'.$pid.'-'.generateUrl_un($product['english_name'])));
		exit;
		if(!$product){
			redirect(url(''));
		}

		//更新产品访问量
		ProductModel::update_product_visit($pid);

		//产品分类
		$categoryInfo = CategoryModel::get_category_info($product['catid']);
		$data['categoryInfo'] = $categoryInfo;
		//品牌
		$brandInfo = CategoryModel::get_category_info($product['brand_id'], CategoryModel::TYPE_BRAND);
		$data['brandInfo'] = $brandInfo;

		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
    	$this->assign('specification', $attributeList);                              // 商品规格
//		dump($properties);exit;
    //图库
		$product['photoList'] = ProductModel::get_product_photo_list($pid);

		//产品批发
//		$product['wholesale'] = PromotionModel::get_product_wholesale_info($pid);


		//相关产品
			$product['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);
		//同类产品
		$similarProductList = ProductModel::get_random_product_list('similar_product', array('catid' => $categoryInfo['id']), 12);
		$data['similarProductList'] = $similarProductList;
		//所有产品
//		$allproductList = ProductModel::get_random_product_list('all_product', array('catid' => $categoryInfo['id'], 'level' => 1), 8);
//		$data['allproductList'] = $allproductList;
		//评论
		$commentlist = CommentModel::get_product_comment_list($pid);
		$data['commentlist'] = $commentlist;

		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
			global $user;
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		$this->assign('comment_display', $comment_display);

		if(!isset($_SESSION['comment_token']) || empty($_SESSION['comment_token'])){
			 import('ORG.Util.String');
			 $_SESSION['comment_token'] = String::randString(16);
		 }
		 $this->assign('comment_token',  $_SESSION['comment_token']);

		//先查询是否审核已开启
		$shaidan_disabled = C('shaidan_disabled');
		//晒单
		$shaidanlist = ProductModel::get_shaidan_list($pid,$shaidan_disabled);
		//print_r($shaidanlist);
		if($shaidanlist) {
			for($i=0;$i<count($shaidanlist);$i++) {
				//关联图片
				$sid = $shaidanlist[$i]['id'];
				$photoList02 = ProductModel::getShaidanPhotoList($shaidanlist[$i]['id']);
				$photoList[$sid] = $photoList02;

			}
			$this->assign('photoList', $photoList);
		}
		$data['shaidanlist'] = $shaidanlist;

		//页面meta
		$data['docTitle'] = $product['meta_title'] ? $product['meta_title'] : $product['name'];
		$data['docKeywords'] = $product['meta_keywords'];
		$data['docDescription'] = $product['meta_description'];

			//当前路径
		$pathArray = array();
		if(isset($categoryInfo) && $categoryInfo){
			$pathArray = CategoryModel::get_category_pos($product['catid']);
		}
		 $this->assign('pathArray', $pathArray);
		$data['productInfo'] = $product;

		if($product['two_suit_sn']){//三件套与两件套关联
			$two_suit_info = ProductModel::get_product_info($product['two_suit_sn']);
			if($two_suit_info){
				 $this->assign('two_suit_info', $two_suit_info);
			}
		}

		$this->assign($data);
		/* 记录浏览历史 */
//		$this->history($pid);
		if($product['attribute_type']==1)
			$this->display('product02');
		else
			$this->display('product');
	}

//url地址重定向
	public function rewrite_info()
	{
		global $user, $language_id;
		$pid = intval($_GET['id']);
		if(!$pid){
			redirect(url(''));
		}
		//将URL大写跳转为小写的URl
		$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
		if(preg_match('/^\/[a-z]+\/[0-9]+-[A-Z]+[a-z]+-[A-Z][a-z]+-[A-Z][a-z]+$/', $the_url)){
			send_http_status(301);
			exit;
		}
		//产品信息
		$product = ProductModel::get_product_info($pid);
		
		if(in_array($product['model_id'],C("3d_model_id"))){
			if($product['model_id']==30){
				redirect(url('/3d-show-shirts'));
			}elseif($product['model_id']==31){
				redirect(url('/3d-show-blazers'));
			}elseif($product['model_id']==32){
				redirect(url('/3d-show-pants'));
			}else{
				redirect(url('/3d-show-shirts'));
			}
		}
		
		$this->assign('product', $product);
		//赠送商品,查找父ID
		$parentid = CategoryModel::get_category_info($product['catid']);
		$this->assign('parentid', $parentid);
        
        //下架商品详情页、定制页301到同类列表页
        if($product['display'] == 0){
        	 $cat_parentid = CategoryModel::get_category_parentid($product['catid']);
        	 $skip_catid = $cat_parentid > 0 ? $cat_parentid : $product['catid'];
             send_http_status(301);
             redirect(CategoryModel::get_category_one_url($skip_catid));
             exit;
        }        
        

		//产品详情页推荐商品
		 $productListPid = ProductModel::ProductRecommend($product['model_id'],$pid);
		 $ID = array();
		 foreach($productListPid as $v){
		 	$ID[] = $v['id'];
		 }
		 $productList = ProductModel::get_product_collection_list($ID);
		 $this->assign('productList', $productList);

		//面料名称，价格，说明
		$product_db = D('product');
		$product_db->join('product_items ON product_items.product_id = product.id');
		$fabricinfo = $product_db->where(array('model_id'=>19,'language_id'=>$language_id))->find();
		$this->assign('fabricinfo',$fabricinfo);

		if(!$product){
			redirect(url(''));
		}
        //省略url中问号后面的参数
        $wenhao_index = strpos($_SERVER['REQUEST_URI'],'?');
        if($wenhao_index){
         $_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'],0,$wenhao_index);
        }

         //对url地址进行验证
         if(strtolower($_SERVER['REQUEST_URI']) != strtolower($product['url'])){
            if($pid && $product ){
                 send_http_status(301);
                 redirect($product['url']);
                 exit;
             }
            send_http_status(404);
            $this->display(C('ERROR_PAGE'));
            exit;
         }
         //giftcard 跳到 giftcard 页面
         if($product['catid'] == 2121){
            send_http_status(301);
            $this->redirect('gift/index');
            exit;
         }
         //505 跳到 面料卡 页面
         if($pid == 505){
            send_http_status(301);
            $this->redirect('Customization/fabrics_kit');
            exit;
         }
        
        //更新产品访问量
		ProductModel::update_product_visit($pid);

        //获取用户浏览历史
        $historyProduct = ProductModel::getBrowsingHistoryProduct();
        $this->assign('historyProduct', $historyProduct); 
        //写入用户浏览历史
        ProductModel::insertBrowsingHistoryProduct($pid);

        //秒杀活动在页面显示抢到价格
		if(UserModel::logged()) {
            $seckill_result = D('seckill_result')->field('id, price')->where(array('user_id'=>$user['id'], 'pid'=>$pid, 'used'=>0))->find();
            if($seckill_result){
            	$product['original_price'] = $product['advanced_price'];
                $product['advanced_price'] = $seckill_result['price'];
                $product['custom_price'] = $seckill_result['price'];
            }
         }

		//产品分类
		$categoryInfo = CategoryModel::get_category_info($product['catid']);
        //麦迪莱登跳转到首页 预约量体跳转到首页
        if($categoryInfo['parentid']==2174||$categoryInfo['id']==2174||$product['catid']==2179){
            $this->redirect('/');
            exit;
        }
		$data['categoryInfo'] = $categoryInfo;
		//品牌
		$brandInfo = CategoryModel::get_category_info($product['brand_id'], CategoryModel::TYPE_BRAND);
		$data['brandInfo'] = $brandInfo;

		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
		//dump($attributeList);exit;
    	$this->assign('specification', $attributeList);                              // 商品规格

        //图库
		$product['photoList'] = ProductModel::get_product_photo_list($pid);
        //背景图片
		$product['backgroundPhotoList'] = ProductModel::get_product_background_photo_list($pid);
		//产品批发
        //$product['wholesale'] = PromotionModel::get_product_wholesale_info($pid);
		

		//相关产品
		$product['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);
		//同类产品
		$similarProductList = ProductModel::get_random_product_list('similar_product', array('catid' => $categoryInfo['id']), 12);
		$data['similarProductList'] = $similarProductList;
		//所有产品
//		$allproductList = ProductModel::get_random_product_list('all_product', array('catid' => $categoryInfo['id'], 'level' => 1), 8);
//		$data['allproductList'] = $allproductList;
		//评论
		$commentlist = CommentModel::get_product_comment_list($pid);
		$data['commentlist'] = $commentlist;

		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
			global $user;
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		$this->assign('comment_display', $comment_display);

		if(!isset($_SESSION['comment_token']) || empty($_SESSION['comment_token'])){
			 import('ORG.Util.String');
			 $_SESSION['comment_token'] = String::randString(16);
		 }
		 $this->assign('comment_token',  $_SESSION['comment_token']);

		//先查询是否审核已开启
		$shaidan_disabled = C('shaidan_disabled');
		//晒单
		$shaidanlist = ProductModel::get_shaidan_list($pid,$shaidan_disabled);
		//print_r($shaidanlist);
		if($shaidanlist) {
			for($i=0;$i<count($shaidanlist);$i++) {
				//关联图片
				$sid = $shaidanlist[$i]['id'];
				$photoList02 = ProductModel::getShaidanPhotoList($shaidanlist[$i]['id']);
				$photoList[$sid] = $photoList02;

			}
			$this->assign('photoList', $photoList);
		}
		$data['shaidanlist'] = $shaidanlist;

		//页面meta
		$data['docTitle'] = $product['meta_title'] ? $product['meta_title'] : $product['name'];
		$data['docKeywords'] = $product['meta_keywords'];
		$data['docDescription'] = $product['meta_description'];

			//当前路径
		$pathArray = array();
		if(isset($categoryInfo) && $categoryInfo){
			$pathArray = CategoryModel::get_category_pos($product['catid']);
		}
		 $this->assign('pathArray', $pathArray);
		$data['productInfo'] = $product;

		if($product['two_suit_sn']){//三件套与两件套关联
			$two_suit_info = ProductModel::get_product_info($product['two_suit_sn']);
			if($two_suit_info){
				 $this->assign('two_suit_info', $two_suit_info);
			}
		}
		//给营销用的
		$yx_prodid = "'".$product['id']."'";
		if($product['attribute_type']==1){
            $yx_totalvalue = $product['advanced_price'];
		}else{
			$yx_totalvalue = $product['custom_price'];
		}
        $this->assign('yx_prodid', $yx_prodid);
        $this->assign('yx_totalvalue', $yx_totalvalue);

        //评论 和 星星
        $productComment = array(
            'count' => CommentModel::get_product_comment_count($product['id']),
            'star'  => CommentModel::get_product_comment_star($product['id']),
        	);
        $this->assign('productComment', $productComment);

		$this->assign($data);
		/* 记录浏览历史 */
//		$this->history($pid);

		if($product['attribute_type']==1){
			$this->display('product02');
		}else{
			//根据产品类型查出 --选尺码购物
			$size_type_arr = C('m_data');
            //礼服特殊处理
			if($product['model_id'] == 23){
				$cat_parentid = CategoryModel::get_category_parentid($product['catid']);
				if($product['catid'] == 2061 || $cat_parentid == 2061){
                      $product['model_id'] = 233;
				}else{
					  $product['model_id'] = 232;
				}
			}
			$size_type = $size_type_arr[$product['model_id']];
			$size_type_res = array();
			foreach ($size_type as $key => $val) {
				$notm_where = array(
                    'size_type' => $val,
					'status'=>1
					);
				$size_data_notm = D('size_data_notm')->field('DISTINCT(size)')
				                  ->where($notm_where)->select();
				$size_type_res[$val] = $size_data_notm;
			}

			$this->assign('size_type_res', $size_type_res);

			$this->assign('user', $user);
			$this->display('product');			
		}
	}
		//产品详细页
	/**
	 * 初级定制
	 */
	public function custom()
	{
		global $language_id;

		$pid = intval($_GET['id']);
		$cid = intval($_GET['cid']);
		$productInfo = ProductModel::get_product_info($pid);

		$cid_url = $cid>0 ? "?cid=".$cid : "";
		//url 301重定向
		send_http_status(301);
		redirect(url('/custom/'.$pid.'-'.generateUrl_un($productInfo['english_name'])).$cid_url);
		exit;
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

		if($productInfo['attribute_type'] != 2){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

		$data['productInfo'] = $productInfo;

		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
		//dump($attributeList);exit;
		$count_attributeList = count($attributeList)-4;

		//选择了的属性
			if(session('custom')){
				$customArray = unserialize(base64_decode(session('custom')));
			}

			if($attributeList && ( !( isset($customArray) && $customArray[$pid]) ) ){
//				if($attributeList){
			$attrSelect = array();
			foreach ($attributeList as $attr_id => $attr){
				if(in_array($attr_id,array(54,55,59,91,70,71,74,120,112,113,116,118,114,115,57,58,66,67,64,65))){//去除笔袋，more option 袖衬贴布 插花眼等的默认值
					continue;
				}
				foreach ($attr['values'] as $attrv){
					if($attr['default'] == $attrv['product_attr_id']){
						//修复BUG 后台定制属性 有值 但名字为空
						$attrSelect[$attr_id] = $attr_id.':'.$attr['name'].':'.$attrv['product_attr_id'] .':'. $attrv['label'] .':'. $attrv['attr_value_id'];
						break;
					}
				}
			}
			$custom_cookie = base64_encode(serialize(array($pid => $attrSelect)));
			session('custom', $custom_cookie);
		}
		elseif( $customArray[$pid] ){
			$attrSelect = array();
			foreach ($customArray[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				if($attrArr[2]==''){
					$attributeList[$attr_id]['default_value'] = $attrArr[3];
				}else{
					$attributeList[$attr_id]['default'] = $attrArr[2];
				}
			}
		}
//		dump($attributeList);exit;
		$this->assign('attributeList', isset($attributeList) ? $attributeList : null);


		//相关产品
		$data['productInfo']['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);
		if($cid){
			$data['cid'] = $cid;
		}
		$this->assign($data);
		$this->assign('count_attributeList',$count_attributeList);
		if($productInfo['model_id'] == 2){
			$this->display('product_suits_custom');
		}elseif($productInfo['model_id'] == 18){
			$this->display('product_three_suits_custom');
		}elseif($productInfo['model_id'] == 1){
			$this->display('product_jacket_custom');
		}elseif($productInfo['model_id'] == 14){
			$this->display('product_shirt_custom');
		}

	}
	//url重定向
	public function rewrite_custom()
	{
		global $user, $language_id;
		$pid = intval($_GET['id']);
		$cid = intval($_GET['cid']);
		$buy = strval($_GET['buy']);

		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

		if($productInfo['attribute_type'] != 2){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

        //下架商品详情页、定制页301到同类列表页
        if($productInfo['display'] == 0){
        	 $cat_parentid = CategoryModel::get_category_parentid($productInfo['catid']);
        	 $skip_catid = $cat_parentid > 0 ? $cat_parentid : $productInfo['catid'];
             send_http_status(301);
             redirect(CategoryModel::get_category_one_url($skip_catid));
             exit;
        }  		

		$data['productInfo'] = $productInfo;

        $attrData = '';
		//放入购物车后修改的商品取表里的attr
		if($cid > 0){
			$cart_products_db = D('cart_products');
			if($_GET['partner'] == 1){
                $cart_products_db = D('cart_products_partner');
			}
			$cart_product = $cart_products_db->field('add_time, attr, measure_id')->where(array('id' => $cid))->find();
			if($cart_product && ($cart_product['add_time'] > strtotime('2014-09-25 18:00:20'))){
				$attrData = $cart_product['attr'];
		    }
		}

        //读取定制商品的定制信息,同时放入缓存
        list($attributeList, $size_type, $attrOptionArr) = CartModel::cart_attr_show($pid, $attrData, true);
	
        //休闲单西 水洗单西  羊绒衫 皮鞋 取消绣字
        if(in_array($productInfo['id'],C('clear_xiuzi_id'))||in_array($productInfo['catid'],C('clear_xiuzi_catid'))){
            $attrOptionArr['fontLocation'] = 0;
            unset($attributeList[9999]);
        }
        // echo "<pre />";
        // print_r($attributeList);exit;

        $buy_type = 0;
        if($buy == 'now'){
    	    //根据产品类型查出  --选尺码购物
			$size_type_arr = C('m_data');
			$size_type = $size_type_arr[$productInfo['model_id']];
			$msizedata = array();
			foreach ($size_type as $val) {
			    $get_msizedata = $this->_request('m'.$val);
			    if(!$get_msizedata){
			    	$msizedata = NULL;
			    	break;
			    }
			    $msizedata[$val] = $get_msizedata;

			}
		    $msizedata = base64_encode(serialize($msizedata));
			//无绣字直接加入购物车
            if($attrOptionArr['fontLocation'] == 0){
            	$msizedataurl = '/msizedata/'.$msizedata;
				if($productInfo['model_id']==27){ //如果是羊绒衫  成衣不定制   直接接入购物车
					$url = url('/cart/productpost/custom/1/pid/'.$pid.$msizedataurl);
				}else{
					$url = url('/cart/productpost/custom/1/pid/'.$pid.'/custom_type/2'.$msizedataurl);
				}
                header("location:".$url);exit;
            }
            $buy_type = 1;
            $this->assign('msizedata', $msizedata); 
        }
        //cid>0的情况下，查询是否选尺码购买
        if($cid>0 && $cart_product['measure_id']>0 && $productInfo['model_id'] != 25){
            $measure = MeasureModel::get_measure_one(array('id' => $cart_product['measure_id']));
            if($measure['m_status'] == 5){
            	$buy = 'now'; //属于快速购买
            	$this->assign('msizedata', base64_encode($measure['measure_data'])); 
            }
        }

        //记录定制统计
        $set_ipcount = array(
                       'pid' => $pid,
	                   'custom' => $buy_type,
	                   'cartid' => $cid,
                       );
        $this->assign('ipcount_id', ProductModel::set_ipcount('add',$set_ipcount));

        $this->assign('quickbuy', $buy);
        $this->assign('custom_more', $attrOptionArr['custom_more']);
        $this->assign('sel_custom_val', $user['sid']);
		

		$this->assign('attributeList', isset($attributeList) ? $attributeList : null);
		$this->assign('arrtlist', $attrOptionArr['arrtlist']);

		//鞋子的尺码
        $shoe_size=ProductModel::get_shoes_size_data($productInfo['shoes_size_max'],$productInfo['shoes_size_min'],$productInfo['shoes_size_del']);
        $this->assign('shoe_size', $shoe_size);
         //相关产品
		$data['productInfo']['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);
		if($cid){
			$data['cid'] = $cid;
		}
		$this->assign($data);
		
		//判断属性的显隐
		$show_attr_array=array();
		if($productInfo['show_attr_ids']){
			$show_attr_array=explode(",",$productInfo['show_attr_ids']);
		}
		
		$this->assign("show_attr_array",$show_attr_array);
		
		//$this->assign('count_attributeList',$count_attributeList);
        if($_GET['partner'] == 1){
            $this->display('custom_product_partner');exit;
        }else{
        	$this->display('custom_product');exit;
        }


	}

    //记录定制统计 是否点击了more_option
	public function ipcount(){
        $ipcount_id = intval($_POST['ipcount_id']);
        if($ipcount_id > 0){
	        //记录定制统计
	        $set_ipcount = array(
	                       'id' => $ipcount_id,
	                       );
            ProductModel::set_ipcount('add_option',$set_ipcount);
        }
	}

	//定制总结(西服) 两件套
	public function custom_submit_ajax(){
		global $language_id;
		$pid = $_REQUEST['pid'];
		$custom = unserialize(base64_decode(session('custom')));
		$res = array(
			'error' => 0,
			'html' => null,
			'htm2' => null,
		);
		if($pid){
			$custom_html = '<ul>';
			foreach ($custom[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];
				if($attrArr[0] == 54 && $attrArr[1] != ''){
					$attrArrId[] = $attrArr[4];
				}else{
					$attrArrId[] = $attrArr[0];
					$attrArrId2[$attrArr[0]] = $attrArr[4];
				}
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				/*print_r($attrArr);
				echo "<br>";*/
				if($attrArr[0] != '54' || $attrArr[0] != '55' || $attrArr[0] != '91' || $attrArr[0] != '57' || $attrArr[0] != '58' ){
					//print_r($attrArr[0]);

					if($attrArr[0]==54 || $attrArr[0]==91 || $attrArr[0]==55 || $attrArr[0]==57 || $attrArr[0]==58 || $attrArr[0]==59 || $attrArr[0]==30 || $attrArr[0]==31 || $attrArr[0]==32 || $attrArr[0]==33 || $attrArr[0]==39){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr_name.':</strong>';
						$custom_html .= '<img src="'.$attribute_values_info['attr_img'].'" />';
						$custom_html .= '<span>'.$attrArr[3].'</span>';
						$custom_html .= '</li>';
					}
				}


			}

			//产品属性
			$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
			if($attributeList){
				$custom_html2 = '<div class="left" style="margin-right:10px;">';
				$custom_html2 .= '	<dl>';
				$i=1;
				foreach ($attributeList as $attr_id => $attr){
					//笔袋
					if($attr_id==55){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/bidai.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==91){//插花眼
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/chahuayan.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==54){//more option
						foreach($attributeList[$attr_id]['values'] as $attrv2){
							$i++;
							if(in_array($attrv2['attr_value_id'],$attrArrId)){
								$attrValueName = L('yes');
							}else{
								continue;
								$attrValueName = L('no');
							}
							/*
							$custom_html2 .= '   <dt>';
							$custom_html2 .= '	<span>'.$attrv2['label'].'：</span><span>'.$attrValueName.'</span>';
							$custom_html2 .= '   </dt>';
							if($i==7){
								$custom_html2 .= '</dl> ';
								$custom_html2 .= '</div> ';
								$custom_html2 .= '<div class="middle"></div> ';
								$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
								$custom_html2 .= '<dl> ';
							}*/
							$custom_html .= '<li>';
							$custom_html .= '<strong>'.$attrv2['label'].':</strong>';
							$custom_html .= '<img src="'.$attrv2['image'].'" />';
							$custom_html .= '<span>'.$attrValueName.'</span>';
							$custom_html .= '</li>';
						}

					}elseif($attr_id==59){//袖肘贴布
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/zhoucuntiebu.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==57){//锁眼线，钉扣线颜色
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/suoyanxian.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==58){//领底尼颜色
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/lingdini.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}
				}
				$custom_html2 .= ' </dl>';
				$custom_html2 .= ' </div>';
			}
			$custom_html .= '</ul>';

			$res = array(
				'error' => 1,
				'html'  => $custom_html,
				'htm2' => '',
			);
		}
		echo json_encode($res);
		//echo $custom_html."<br>";
		//echo $custom_html2;
	}

	//定制总结(西服三件套)
	public function custom_submit_ajax_three(){
		global $language_id;
		$pid = $_REQUEST['pid'];
		$custom = unserialize(base64_decode(session('custom')));
		$res = array(
			'error' => 0,
			'html' => null,
			'htm2' => null,
		);

		if($pid){
			$custom_html = '<ul>';
			foreach ($custom[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];
				if($attrArr[0] == 112 && $attrArr[1] != ''){
					$attrArrId[] = $attrArr[4];
				}else{
					$attrArrId[] = $attrArr[0];
					$attrArrId2[$attrArr[0]] = $attrArr[4];
				}
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				/*print_r($attrArr);
				echo "<br>";*/
				if($attrArr[0] != '112' || $attrArr[0] != '113' || $attrArr[0] != '118' || $attrArr[0] != '114' || $attrArr[0] != '115' ){
					//print_r($attrArr[0]);

					if($attrArr[0]==112 || $attrArr[0]==113 || $attrArr[0]==118 || $attrArr[0]==114 || $attrArr[0]==115 || $attrArr[0]==116 || $attrArr[0]==99 || $attrArr[0]==100 || $attrArr[0]==101 || $attrArr[0]==102 || $attrArr[0]==39){continue;}//39貌似无效
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr_name.':</strong>';
						$custom_html .= '<img src="'.$attribute_values_info['attr_img'].'" />';
						$custom_html .= '<span>'.$attrArr[3].'</span>';
						$custom_html .= '</li>';
					}
				}


			}

			//产品属性
			$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
			if($attributeList){
				$custom_html2 = '<div class="left" style="margin-right:10px;">';
				$custom_html2 .= '	<dl>';
				$i=1;
				foreach ($attributeList as $attr_id => $attr){

					if($attr_id==113){//笔袋
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/bidai.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==118){//插花眼
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/chahuayan.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==112){//more option
						foreach($attributeList[$attr_id]['values'] as $attrv2){
							$i++;
							if(in_array($attrv2['attr_value_id'],$attrArrId)){
								$attrValueName = L('yes');
							}else{
								continue;
								$attrValueName = L('no');
							}

							$custom_html .= '<li>';
							$custom_html .= '<strong>'.$attrv2['label'].':</strong>';
							$custom_html .= '<img src="'.$attrv2['image'].'" />';
							$custom_html .= '<span>'.$attrValueName.'</span>';
							$custom_html .= '</li>';
						}

					}elseif($attr_id==116){//袖肘贴布
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/zhoucuntiebu.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==114){//锁眼线，钉扣线颜色 领底尼颜色
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/suoyanxian.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif( $attr_id==115){//锁眼线，钉扣线颜色 领底尼颜色
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/lingdini.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}
				}
				$custom_html2 .= ' </dl>';
				$custom_html2 .= ' </div>';
			}
			$custom_html .= '</ul>';

			$res = array(
				'error' => 1,
				'html'  => $custom_html,
				'htm2' => '',//取消more option分开显示
			);
		}
		echo json_encode($res);
		//echo $custom_html."<br>";
		//echo $custom_html2;
	}

	//定制总结(夹克) 单西
	public function jacket_submit_ajax(){
		global $language_id;
		$pid = $_REQUEST['pid'];
		$custom = unserialize(base64_decode(session('custom')));
		$res = array(
			'error' => 0,
			'html' => null,
			'htm2' => null,
		);

		if($pid){
			$custom_html = '<ul>';
			foreach ($custom[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];
				if($attrArr[0] == 70 && $attrArr[1] != ''){
					$attrArrId[] = $attrArr[4];
				}else{
					$attrArrId[] = $attrArr[0];
					$attrArrId2[$attrArr[0]] = $attrArr[4];
				}
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				/*print_r($attrArr);
				echo "<br>";*/
				if($attrArr[0] != '70' || $attrArr[0] != '71' || $attrArr[0] != '120' || $attrArr[0] != '72' || $attrArr[0] != '73' ){
					//print_r($attrArr[0]);

					if($attrArr[0]==70 || $attrArr[0]==71 || $attrArr[0]==120 || $attrArr[0]==72 || $attrArr[0]==73 || $attrArr[0]==74 || $attrArr[0]==64 || $attrArr[0]==65 || $attrArr[0]==66 || $attrArr[0]==67 || $attrArr[0]==69){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr_name.':</strong>';
						$custom_html .= '<img src="'.$attribute_values_info['attr_img'].'" />';
						$custom_html .= '<span>'.$attrArr[3].'</span>';
						$custom_html .= '</li>';
					}
				}


			}

			//产品属性
			$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
			if($attributeList){
				$custom_html2 = '<div class="left" style="margin-right:10px;">';
				$custom_html2 .= '	<dl>';
				$i=1;
				foreach ($attributeList as $attr_id => $attr){
					if($attr_id==71){//笔袋
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/bidai.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==120){//插花眼
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/chahuayan.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==70){//more option
						foreach($attributeList[$attr_id]['values'] as $attrv2){
							$i++;
							if(in_array($attrv2['attr_value_id'],$attrArrId)){
								$attrValueName = L('yes');
							}else{
								continue;
								$attrValueName = L('no');
							}
							/*
							$custom_html2 .= '   <dt>';
							$custom_html2 .= '	<span>'.$attrv2['label'].'：</span><span>'.$attrValueName.'</span>';
							$custom_html2 .= '   </dt>';
							if($i==7){
								$custom_html2 .= '</dl> ';
								$custom_html2 .= '</div> ';
								$custom_html2 .= '<div class="middle"></div> ';
								$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
								$custom_html2 .= '<dl> ';
							}*/
							$custom_html .= '<li>';
							$custom_html .= '<strong>'.$attrv2['label'].':</strong>';
							$custom_html .= '<img src="'.$attrv2['image'].'" />';
							$custom_html .= '<span>'.$attrValueName.'</span>';
							$custom_html .= '</li>';
						}

					}elseif($attr_id==74){//袖肘贴布
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
						if($i==7){
							$custom_html2 .= '</dl> ';
							$custom_html2 .= '</div> ';
							$custom_html2 .= '<div class="middle"></div> ';
							$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
							$custom_html2 .= '<dl> ';
						}*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/zhoucuntiebu.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';

					}elseif($attr_id==72){//锁眼线
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
						if($i==7){
							$custom_html2 .= '</dl> ';
							$custom_html2 .= '</div> ';
							$custom_html2 .= '<div class="middle"></div> ';
							$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
							$custom_html2 .= '<dl> ';
						}*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/statics/front/images/custom/suoyanxian.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}elseif($attr_id==73){//领底尼颜色
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							continue;
							$attrValueName = L('no');
						}
						/*
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
						if($i==7){
							$custom_html2 .= '</dl> ';
							$custom_html2 .= '</div> ';
							$custom_html2 .= '<div class="middle"></div> ';
							$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
							$custom_html2 .= '<dl> ';
						}*/
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr['name'].':</strong>';
						$custom_html .= '<img src="/web/Tpl/default/images/custom/lingdinni.jpg" />';
						$custom_html .= '<span>'.$attrValueName.'</span>';
						$custom_html .= '</li>';
					}
				}
				$custom_html2 .= ' </dl>';
				$custom_html2 .= ' </div>';
			}
			$custom_html .= '</ul>';

			$res = array(
				'error' => 1,
				'html'  => $custom_html,
				'htm2' => $custom_html2,
			);
		}
		echo json_encode($res);
		//echo $custom_html."<br>";
		//echo $custom_html2;
	}

	//定制总结(寸衫)
	public function shirt_submit_ajax(){
		global $language_id;
		$pid = $_REQUEST['pid'];
		$custom = unserialize(base64_decode(session('custom')));
		$res = array(
			'error' => 0,
			'html' => null,
		);

		if($pid){
			$custom_html = '<ul>';
			foreach ($custom[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];

				$attrArrId[] = $attrArr[0];
				$attrArrId2[$attrArr[0]] = $attrArr[4];
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				/*print_r($attrArr);
				echo "<br>";*/
				if($attrArr[0] != '80' || $attrArr[0] != '81' || $attrArr[0] != '82' || $attrArr[0] != '83' ){
					//print_r($attrArr[0]);

					if($attrArr[0]==80 || $attrArr[0]==81 || $attrArr[0]==82 || $attrArr[0]==83){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<strong>'.$attr_name.':</strong>';
						$custom_html .= '<img src="'.$attribute_values_info['attr_img'].'" />';
						$custom_html .= '<span>'.$attrArr[3].'</span>';
						$custom_html .= '</li>';
					}
				}
			}
			$custom_html .= '</ul>';

			$res = array(
				'error' => 1,
				'html'  => $custom_html,
			);
		}
		echo json_encode($res);
		//echo $custom_html."<br>";
		//echo $custom_html2;
	}

	//定制选择
	public function select_custom()
	{
		global $user,$language_id;
		$res = array('state'=>1,"msg"=>"");
		$pid = intval($_REQUEST['pid']);
		if($pid == 0){
		    echo 'error'; exit;
		}
		$dis = intval($_REQUEST['dis']);
		$attr_id = intval($_REQUEST['attr_id']);
		$attr_value_id = trim($_REQUEST['attr_value_id']);
		$attrs = trim($_REQUEST['attrs']);
		$special_request = trim($_REQUEST['special_request']);
		$custom = session('custom');
		$customArray = unserialize(base64_decode($custom));
        //如果session里的定制选项跟缓存里的不同，则用缓存里的
        $sid = trim($_REQUEST['sel_custom_val']);
        $sel_custom_val = S('sel_custom_val_'.$sid);
        if($sel_custom_val && is_array($sel_custom_val)){
        	if(!isset($customArray[$pid]) || (json_encode($sel_custom_val[$pid]) != json_encode($customArray[$pid]))){
        		$customArray[$pid] = $sel_custom_val[$pid];
        	}
        }
		unset($customArray["special_request"]);
		unset($customArray["special_request_".$pid]);
		$customArray["special_request_".$pid]= $special_request;
		if($attrs){
			$attrs_arr=explode(":",$attrs);
			if(count($attrs_arr)>4){
				//如果插花眼是无，则插花眼撞色无
				//两件套
				if($attrs_arr[0]==91 && $attrs_arr[4]==1147){
					unset($customArray[$pid][57]);
				}
				//三件套
				if($attrs_arr[0]==118 && $attrs_arr[4]==1148){
					unset($customArray[$pid][114]);
				}
				//单西 
				if($attrs_arr[0]==120 && $attrs_arr[4]==864){
					unset($customArray[$pid][72]);
				}
			
				//马甲领型U型领，则无马甲手巾袋
				//三件套
				
				if($attrs_arr[0]==105 && $attrs_arr[4]==316){
					unset($customArray[$pid][202]);
				}
				//马甲
				if($attrs_arr[0]==126 && $attrs_arr[4]==604){
					unset($customArray[$pid][229]);
				}
				//衬衫撞色
				if($attrs_arr[0]==228 && $attrs_arr[4]==1273){
					unset($customArray[$pid][89]);
				}
				//领型为909,1304,1125,1283,则显示领口部撞色
				if($attrs_arr[0]==56 && !in_array($attrs_arr[4],array(909,1304,1125,1283))){
					unset($customArray[$pid][230]);
				}
				//袖口为95,则显示袖口部撞色
				if($attrs_arr[0]==75 && !in_array($attrs_arr[4],array(905))){
					unset($customArray[$pid][231]);
				}
				//衬衫撞色
				if($attrs_arr[0]==149 && $attrs_arr[4]==833){
					unset($customArray[$pid][75]);
				}
				
			}
		}
		
		if($attr_value_id){
			if($attr_value_id==762){
		        unset($customArray[$pid][136]);
		    }
		    if($attr_value_id==136){
		        unset($customArray[$pid][762]);
		    }
			if($dis>0){
				//$customArray[$pid][$attr_value_id] = '';
				unset($customArray[$pid][$attr_value_id]);
			}else{
				$customArray[$pid][$attr_value_id] = $attrs;
			}

		}else{
			if($dis>0){
                unset($customArray[$pid][$attr_id]);
			}else{
			    if($attr_id > 0) $customArray[$pid][$attr_id] = $attrs;
			}
		}
		/*
		if($attrs &&  in_array($attr_id,array(245,246,244,243,240,241,242))){
			$attrs = str_replace(":::","::",$attrs);
			$attrs_arr = explode("::",$attrs);
			$supplier_name = $attrs_arr[1];
			if(!empty($supplier_name)){
				$supplier_model = new SuppliersModel();
				$suppliers = $supplier_model->getSuppliersByName($supplier_name);
				if(empty($suppliers)){
					$res['state'] = 0;
					$res['msg'] = "面料编号不存在！";
				}
			}
		}
		*/
		session('custom', base64_encode(serialize($customArray)));
		$cacheId = 'sel_custom_val_'.$user['sid'];
		S($cacheId, $customArray);
		$res['sid'] = $user['sid'];
		echo json_encode($res);
	}
	
	public function select_custom_new()
	{
		global $user,$language_id;
		$pid = intval($_REQUEST['pid']);
		if($pid == 0){
		    echo 'error'; exit;
		}
		$dis = intval($_REQUEST['dis']);
		$attr_id = intval($_REQUEST['attr_id']);
		$attr_value_id = trim($_REQUEST['attr_value_id']);
		$attrs = trim($_REQUEST['attrs']);
		$custom = session('custom_admin');
		$customArray = unserialize(base64_decode($custom));
        //如果session里的定制选项跟缓存里的不同，则用缓存里的
        $sid = trim($_REQUEST['sel_custom_val']);
        $sel_custom_val = S('sel_custom_val_'.$sid);
        if($sel_custom_val && is_array($sel_custom_val)){
        	if(!isset($customArray[$pid]) || (json_encode($sel_custom_val[$pid]) != json_encode($customArray[$pid]))){
        		$customArray[$pid] = $sel_custom_val[$pid];
        	}
        }
		if($attrs){
			$attrs_arr=explode(":",$attrs);
			if(count($attrs_arr)>4){
				//如果插花眼是无，则插花眼撞色无
				//两件套
				if($attrs_arr[0]==91 && $attrs_arr[4]==1147){
					unset($customArray[$pid][57]);
				}
				//三件套
				if($attrs_arr[0]==118 && $attrs_arr[4]==1148){
					unset($customArray[$pid][114]);
				}
				//单西 
				if($attrs_arr[0]==120 && $attrs_arr[4]==864){
					unset($customArray[$pid][72]);
				}
			
				//马甲领型U型领，则无马甲手巾袋
				//三件套
				
				if($attrs_arr[0]==105 && $attrs_arr[4]==316){
					unset($customArray[$pid][202]);
				}
				//马甲
				if($attrs_arr[0]==126 && $attrs_arr[4]==604){
					unset($customArray[$pid][229]);
				}
				//衬衫撞色
				if($attrs_arr[0]==228 && $attrs_arr[4]==1273){
					unset($customArray[$pid][89]);
				}
				//领型为909,1304,1125,1283,则显示领口部撞色
				if($attrs_arr[0]==56 && !in_array($attrs_arr[4],array(909,1304,1125,1283))){
					unset($customArray[$pid][230]);
				}
				//袖口为95,则显示袖口部撞色
				if($attrs_arr[0]==75 && !in_array($attrs_arr[4],array(905))){
					unset($customArray[$pid][231]);
				}
				
			}
		}
		
		if($attr_value_id){
			if($attr_value_id==762){
		        unset($customArray[$pid][136]);
		    }
		    if($attr_value_id==136){
		        unset($customArray[$pid][762]);
		    }
			if($dis>0){
				//$customArray[$pid][$attr_value_id] = '';
				unset($customArray[$pid][$attr_value_id]);
			}else{
				$customArray[$pid][$attr_value_id] = $attrs;
			}

		}else{
			if($dis>0){
                unset($customArray[$pid][$attr_id]);
			}else{
			    if($attr_id > 0) $customArray[$pid][$attr_id] = $attrs;
			}
		}
	
		session('custom_admin', base64_encode(serialize($customArray)));

		$cacheId = 'sel_custom_val_'.$sid;
		S($cacheId, $customArray);
		echo $sid;
	}
	/**
	 * 高级定制
	 */
	public function advanced_custom()
	{
		$pid = intval($_GET['id']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

		$data['productInfo'] = $productInfo;

		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0));// 获得商品的规格和属性
		$count_attributeList = count($attributeList)-4;
		//dump($attributeList);exit;

		//选择了的属性
		if(session('custom')){
			$customArray = unserialize(base64_decode(session('custom')));
		}

		if($attributeList && ( !( isset($customArray) && $customArray[$pid]) ) ){
//				if($attributeList){
			$attrSelect = array();
			foreach ($attributeList as $attr_id => $attr){
				foreach ($attr['values'] as $attrv){
					if($attr['default'] == $attrv['product_attr_id']){
						$attrSelect[$attr_id] = $attr_id.':'.$attr['attr_name'].':'.$attrv['product_attr_id'] .':'. $attrv['label'];
						break;
					}
				}
			}
			$custom_cookie = base64_encode(serialize(array($pid => $attrSelect)));
			session('custom', $custom_cookie);
		}
		elseif( $customArray[$pid] ){
			$attrSelect = array();
			foreach ($customArray[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				if($attrArr[2]==''){
					$attributeList[$attr_id]['default_value'] = $attrArr[3];
				}else{
					$attributeList[$attr_id]['default'] = $attrArr[2];
				}
			}
		}
//		dump($attributeList);exit;
		$this->assign('attributeList', isset($attributeList) ? $attributeList : null);


		//相关产品
		$data['productInfo']['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);
		$this->assign($data);
		$this->assign('count_attributeList',$count_attributeList);
		$this->display('product_advanced');
	}

	//写评论
	public function comment()
	{
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}
		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
			global $user;
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		if(!$comment_display){
			$this->error(L('_ILLEGAL_OPERATION_'), url('product/info', 'id='.$pid));
		}
		//提交
		if(isset($_POST['dosubmit']))
		{
			if($_POST['comment_token'] != $_SESSION['comment_token'])
			{
				$this->error(L('_OPERATION_FAIL_'), url('product/comment', 'pid='.$pid));
			}
			if(empty($_POST['username']) || empty($_POST['email']) || empty($_POST['title']) || empty($_POST['content'])){
				$this->error(L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_'), url('product/comment', 'pid='.$pid));
			}


			$_POST['product_id'] = $pid;
			if(CommentModel::insert_comment($_POST)){
				$this->success(L('_OPERATION_SUCCESS_'), url('product/info', 'id='.$pid));
			}else{
				$this->error(L('_OPERATION_FAIL_'), url('product/comment', 'pid='.$pid));
			}
		}else{
		//当前路径
    $pathArray = CategoryModel::get_category_pos($productInfo['catid']);
     $this->assign('pathArray', $pathArray);

     if(!isset($_SESSION['comment_token']) || empty($_SESSION['comment_token'])){
	     import('ORG.Util.String');
	     $_SESSION['comment_token'] = String::randString(16);
     }
     $this->assign('comment_token',  $_SESSION['comment_token']);


			$this->assign('productInfo', $productInfo);
			$this->display('comment');
		}
	}

	public function comment_ajax()
	{
		global $user;
		$pid = intval($_GET['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			echo L('_PRODUCT_NO_EXIST_');exit;
		}
		//是否显示评论入口
		$comment_display = C('comment_display');
		//开启
		if($comment_display){
   		$comment_type = C('comment_type');
   		if($comment_type==1 && !UserModel::logged()){
   			$comment_display = 0;
   		}
		}
		if(!$comment_display){
			echo L('_ILLEGAL_OPERATION_');exit;
		}

		//提交
		if(IS_POST)
		{
			$post = $_POST;
			if($post['comment_token'] != $_SESSION['comment_token'])
			{
				echo L('_OPERATION_FAIL_');exit;
			}
			if(empty($post['username']) || empty($post['email']) || empty($post['title']) || empty($post['content'])){
				echo L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_');exit;
			}


			$post['product_id'] = $pid;
			if(CommentModel::insert_comment($post)){
				echo L('_OPERATION_SUCCESS_');exit;
			}else{
				echo L('_OPERATION_FAIL_');exit;
			}
		}
		echo L('_ILLEGAL_OPERATION_');exit;
	}

	public function search()
	{

		$keyword = trim($_GET['keyword']);
		$keyword = str_replace('+', ' ', $keyword);
        //页面关键字
		$data = array(
    		'docTitle' => L('search_docTitle_front').' "'.$keyword.'" '.L('search_docTitle'),
    		'docKeywords' => L('search_meta_keywords'),
    		'docDescription' => '',
    	);
		$this->assign($data);
		
		$where = array();
		if($keyword){
			$searchKeyword = str_replace('color', '', $keyword);
			$searchKeyword = strtolower($searchKeyword);
			if(preg_match('/blazers|blazer/', $searchKeyword))
				$searchKeyword .= ' blazer';
			if(preg_match('/suit|suits/', $searchKeyword))
				$searchKeyword .= ' suit';
			if(preg_match('/men|mens/', $searchKeyword))
				$searchKeyword .= ' men';
			if(preg_match('/Striped|chambray/', $searchKeyword))	
				$searchKeyword .= ' chambray Striped';
			if(preg_match('/shirt|shirts/', $searchKeyword))
				$searchKeyword .= ' shirt';
			if(preg_match('/tuxedo|wedding/', $searchKeyword))	
				$searchKeyword .= ' dress';
			if(preg_match('/2 piece/', $searchKeyword))	
				$searchKeyword .= ' two piece';
			if(preg_match('/3 piece/', $searchKeyword))	
				$searchKeyword .= ' three piece';
			if(preg_match('/2 button/', $searchKeyword))	
				$searchKeyword .= ' two button';	
		    if(preg_match('/3 button/', $searchKeyword))
				$searchKeyword .= ' three button';															
			$where['keyword'] = $searchKeyword;
		}				

		//分页显示数
		$limit_number = C('product_list_num');

		//产品总数
		$productCount = ProductModel::get_product_count($where);
		//分页
		$pageRows = 10;
	    import("ORG.Util.PageListSimple");
	    $Page = new Page($productCount,$pageRows);
	    $pageShow = $Page->show();
		if($productCount){
			$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
		}else{
			$productCount = 0;
			$productListpid = CartModel::Recommend(array());
            $productList = ProductModel::get_product_collection_list($productListpid);
		}
		if(!isset($_GET['p']) || intval($_GET['p']) < 2){
	        //搜索历史
	        $searchHistory = array(
	    	    'keyword' => $keyword,
	    	    'result' => $productCount,
	        	);
	        ProductModel::insertSearchHistory($searchHistory);
		}
        
	    $this->assign('productCount', $productCount);
	    $this->assign('productList', $productList);
	    $this->assign('pageShow', $pageShow);
	    $this->assign('target', '_blank');
	    $this->assign('keyword', $keyword); //搜索条件
		$this->display('product_search_list');
	}

	//搜索历史
	public function searchHistory(){
		$keyword = array();
        $searchHistory = ProductModel::searchHistory($num = 5, $sid = null);
        if($searchHistory){
			foreach ($searchHistory as $key => $value) {
			    $keyword[] = $value['keyword'];
			}
        }
        echo json_encode($keyword);
	}

	//获取商品价格
	public function getPrice()
	{
		global $user;
		$res = array(
			'error' => 0,
			'message' => null,
		);
		$post =(array)json_decode($_POST['post']);
		$pid = intval($post['pid']);
		$attr = $post['attr'];
		$res['qty'] = $qty = intval($post['qty']) ? intval($post['qty']) : 1;

		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$res['message'] = L('_PRODUCT_NO_EXIST_');
			$res['error'] = 1;
		}else{
			$price = ProductModel::get_final_price($pid, $qty, true, $attr);
			$res['message'] = currency($price*$qty);
		}
		echo json_encode($res);
	}

	/**
	 * 获取产品属性
	 */
	public function attribute_ajax()
	{
		global $user;
		$res = array(
			'error' => 0,
			'message' => null,
		);
		$pid = intval($_REQUEST['pid']);
//		$pid = 195200;
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$res['message'] = L('_PRODUCT_NO_EXIST_');
			$res['error'] = 1;

		}else{
			$attributeList = ProductModel::get_product_relation_attribte_list($pid);
			if($attributeList && isset($attributeList['spe'])){
				$res['error'] = -1;
				$res['message'] = $attributeList['spe'];
				$res['content'] = $productInfo['name'];
			}
			//没有属性
			else{
				$cartAction = A('Cart');
				$res = $cartAction->add_product_cart($pid,1,null,true);
			}
		}
		echo json_encode($res);
	}

	/* 记录浏览历史 */
	private function history($goods_id)
	{
		$cook_exp = array(
		'expire' => time() + 3600 * 24 * 30
		);
		if (cookie('think_rongpai_history'))
		{
		    $history = explode(',', cookie('think_rongpai_history'));

		    array_unshift($history, $goods_id);
		    $history = array_unique($history);

		    while (count($history) > 5)
		    {
		        array_pop($history);
		    }

		    cookie('think_rongpai_history',implode(',', $history), $cook_exp);
		}
		else
		{
			cookie('think_rongpai_history',$goods_id, $cook_exp);
		}
	}
	/*清空浏览历史*/
	public function clear_history()
	{
		$cook_exp = array(
		'expire' => time() + 3600 * 24 * 30
		);
		cookie('think_rongpai_history',NULL, $cook_exp);
	}

	//产品详细页
	public function presell()
	{
		$pid = intval($_GET['id']);
		if(!$pid){
			redirect(url(''));
		}
		//产品信息
		$product = ProductModel::get_product_info($pid);
		if(!$product){
			redirect(url(''));
		}
		/*$value = '4_0';
		$arr = explode('_', $value);
		echo count($arr);
		print_r($arr);
		$arr[count($arr)-1];*/

		//更新产品访问量
		ProductModel::update_product_visit($pid);

		//产品分类
		$categoryInfo = CategoryModel::get_category_info($product['catid']);
		$data['categoryInfo'] = $categoryInfo;

    	//图库
		$product['photoList'] = ProductModel::get_product_photo_list($pid);

		//相关产品
		$product['relationList'] = ProductModel::get_product_relation_list($pid, 1, 4);

		//页面meta
		$data['docTitle'] = $product['meta_title'] ? $product['meta_title'] : $product['name'];
		$data['docKeywords'] = $product['meta_keywords'];
		$data['docDescription'] = $product['meta_description'];

		//团购信息
		$wholesale = PromotionModel::get_product_wholesale_info($pid);
		$this->assign('wholesale', $wholesale);

		//尺码对照列表
		$userSizeList = AttributeModel::get_user_size_List();
		$this->assign('userSizeList', $userSizeList);

		$data['productInfo'] = $product;

		$this->assign($data);

		$this->display('product_presell');
	}

	/**
	 * 获取用户选的尺码及相关尺码对照表
	 */
	public function getProductSize()
	{
		$price = 0;
		$product_id = intval($_GET['product_id']);
		$id = intval($_GET['id']);
		if($product_id==0){
			exit(0);
		}
		$product_size = PromotionModel::get_product_size_info($product_id);
		$prices = unserialize($product_size['prices']);
		$prices_size = $prices[$id];
		//统计该尺码的销售数量
		$order_item_db = D('orders_items');
		$order_item_db->field('sum(qty) as sum');
		$order_item_db->where('product_id='.$product_id.' AND chima="'.$prices_size['size'].'"');
		$productSum = $order_item_db->find();
		//$prices_size['qty'] = $prices_size['qty']-$productSum['sum'];
		//$prices_size['num'] = $productSum['sum'];
		if($productSum['sum'] > 0)
			$prices_size['num'] = $productSum['sum'];
		else
			$productSum['sum'] = 0;
		if(isset($prices_size['size'])){

			$pricesSize = explode("/",$prices_size['size']);

			if($pricesSize[0] && $pricesSize[1] && $pricesSize[2]){
				$product_user_size = D('product_user_size');
				$product_user_size->field('circumference,waistline,hipline,shoulder,forepart_length,outside_sleeve');
				$where['height'] = array('elt', $pricesSize[0]);
				$where['height2'] = array('egt', $pricesSize[0]);
				$where['weight'] = array('elt', $pricesSize[1]);
				$where['weight2'] = array('egt', $pricesSize[1]);
				$where['age'] = array('elt', $pricesSize[2]);
				$where['age2'] = array('egt', $pricesSize[2]);

				$product_user_size->where($where);
				$info = $product_user_size->find();
			}

			$size = array(
				'size' 				=> trim($prices_size['size']),
				'qty' 				=> $prices_size['qty'],
				'stock' 			=> $prices_size['stock'],
				'num' 				=> $prices_size['num'],
				'circumference' 	=> $info['circumference'],
				'waistline' 		=> $info['waistline'],
				'hipline' 			=> $info['hipline'],
				'shoulder' 			=> $info['shoulder'],
				'forepart_length' 	=> $info['forepart_length'],
				'outside_sleeve' 	=> $info['outside_sleeve'],
			);
		}
		else{
			exit(0);
		}
		echo json_encode($size);exit;
	}

	public function ajax_category_list()
    {
		$cat_id = $_REQUEST['cid'];
		$result = array(
			'error' => 1,
			'category_list' => null,
			'productList' => null,
			'number' => 0,
		);
		$category_list = CategoryModel::get_category_sub_list($cat_id); //子类列表
		$html = '';
		$html2 = '';
		if($category_list){
			$i = 1;
			foreach ($category_list as $category)
			{
				if($i>5){
					$html .='<li id="catedispaly_'.$i.'" style="display:none;"><a href="'.$category['url'].'">'.$category['name'].'</a></li>';
					$html2 .='<li id="dispaly_'.$i.'" style="display:none;"><a href="'.$category['url'].'"><img src="'.$category['img'].'" /></a></li>';
				}else{
					$html2 .='<li id="dispaly_'.$i.'"><a href="'.$category['url'].'"><img src="'.$category['img'].'" /></a></li>';
					$html .='<li id="catedispaly_'.$i.'"><a href="'.$category['url'].'">'.$category['name'].'</a></li>';
				}
				$i++;
			}

			$result['error'] = '0';
			$result['category_list'] = $html;
			$result['category_img'] = $html2;
			$result['number'] = $i;
		}

		//echo $cat_id;
		echo json_encode($result);exit;
    }

	public function ajax_category_type()
    {
		global $language_id;
		$tid = $_REQUEST['tid'];
		$result = array(
			'error' => 1,
			'category_list' => null,
			'category_style' => null,
			'productList' => null,
			'number' => 0,
		);
		$category_list = CategoryModel::get_category_sub_list($tid,CategoryModel::TYPE_ARTICLE); //子类列表
		$html = '';
		$html2 = '';
		if($category_list){
			$i = 1;
			$html .='<option>'.L('please_choose').'</option>';
			foreach ($category_list as $category)
			{
				$html .='<option value="'.$category['id'].'">'.$category['name'].'</option>';
				$i++;
			}

			$result['error'] = '0';
			$result['category_list'] = $html;
			$result['category_style'] = 1;
			$result['productList'] = $html2;
			$result['number'] = $i;
		}else{
			$i = 1;
			$category_items_db = D('category_items');
			$category_items_db->field('name');
			$categoryInfo = $category_items_db->where(array('category_id' => $tid, 'language_id' => $language_id))->find();
			if(!$categoryInfo){
				$categoryInfo = $category_items_db->where(array('category_id' => $tid, 'language_id' => C('language_id')))->find();
			}

			$where['catid'] = $tid;
			$article_list = ArticleModel::get_article_list($where);
			$html2 .='<option>'.L('please_choose').'</option>';
			foreach ($article_list as $article)
			{
				$html2 .='<option value="'.$article['id'].'">'.$article['name'].'</option>';
				$i++;
			}
			$result['error'] = '0';
			$result['category_list'] = $categoryInfo['name'];
			$result['category_style'] = 0;
			$result['productList'] = $html2;
			$result['number'] = $i;

		}

		//echo $cat_id;
		echo json_encode($result);exit;
    }

	public function ajax_product_info()
    {
		$aid = $_REQUEST['aid'];
		$result = array(
			'error' => 1,
			'name' => null,
			'content' => null,
		);

		$article_info = ArticleModel::get_article_info($aid);

		if($article_info){
			$result['error'] = '0';
			$result['name'] = $article_info['name'];
			$result['content'] = $article_info['description'];
		}
		//echo $cat_id;
		echo json_encode($result);exit;
    }

	//产品定制总结页面
	public function conclusion()
	{
		global $language_id;
		$cart_id = $_REQUEST['cid'];
		$cartInfo = CartModel::get_cart_product_info_by_id($cart_id);
		$pid = $cartInfo['pid'];
		$productInfo = ProductModel::get_product_info($pid);
		$custom = unserialize($cartInfo['attr']);
		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
		if($productInfo['model_id'] == 2){
			$custom_html = '<ul>';
			foreach ($custom as $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];
				if($attrArr[0] == 54 && $attrArr[1] != ''){
					$attrArrId[] = $attrArr[4];
				}else{
					$attrArrId[] = $attrArr[0];
					$attrArrId2[$attrArr[0]] = $attrArr[4];
				}
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				if($attrArr[0] != '54' || $attrArr[0] != '55' || $attrArr[0] != '57' || $attrArr[0] != '58' ){
					if($attrArr[0]==54 || $attrArr[0]==55 || $attrArr[0]==57 || $attrArr[0]==58 || $attrArr[0]==59 || $attrArr[0]==30 || $attrArr[0]==31 || $attrArr[0]==32 || $attrArr[0]==33 || $attrArr[0]==39){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<div class="name">'.$attr_name.':</div>';
						$custom_html .= '<div><img src="'.$attribute_values_info['attr_img'].'" width="110">';
						$custom_html .= '<div class="result">'.$attrArr[3].'</div>';
						$custom_html .= '</div>';
						$custom_html .= '</li>';
					}
				}
			}
			$custom_html .= '</ul>';
			if($attributeList){
				$custom_html2 = '<div class="left" style="margin-right:10px;">';
				$custom_html2 .= '	<dl>';
				$i=1;
				foreach ($attributeList as $attr_id => $attr){
					if($attr_id==55){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
					}elseif($attr_id==54){
						foreach($attributeList[$attr_id]['values'] as $attrv2){
							$i++;
							if(in_array($attrv2['attr_value_id'],$attrArrId)){
								$attrValueName = L('yes');
							}else{
								$attrValueName = L('no');
							}
							$custom_html2 .= '   <dt>';
							$custom_html2 .= '	<span>'.$attrv2['label'].'：</span><span>'.$attrValueName.'</span>';
							$custom_html2 .= '   </dt>';
							if($i==7){
								$custom_html2 .= '</dl> ';
								$custom_html2 .= '</div> ';
								$custom_html2 .= '<div class="middle"></div> ';
								$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
								$custom_html2 .= '<dl> ';
							}
						}

					}elseif($attr_id==59){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
					}elseif($attr_id==57 || $attr_id==58){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
					}
				}
				$custom_html2 .= ' </dl>';
				$custom_html2 .= ' </div>';
			}
		}elseif($productInfo['model_id'] == 1){
			$custom_html = '<ul>';
			foreach ($custom as $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];
				if($attrArr[0] == 70 && $attrArr[1] != ''){
					$attrArrId[] = $attrArr[4];
				}else{
					$attrArrId[] = $attrArr[0];
					$attrArrId2[$attrArr[0]] = $attrArr[4];
				}
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				if($attrArr[0] != '70' || $attrArr[0] != '71' || $attrArr[0] != '72' || $attrArr[0] != '73' ){
					if($attrArr[0]==70 || $attrArr[0]==71 || $attrArr[0]==72 || $attrArr[0]==73 || $attrArr[0]==74 || $attrArr[0]==64 || $attrArr[0]==65 || $attrArr[0]==66 || $attrArr[0]==67 || $attrArr[0]==69){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<div class="name">'.$attr_name.':</div>';
						$custom_html .= '<div><img src="'.$attribute_values_info['attr_img'].'" width="110">';
						$custom_html .= '<div class="result">'.$attrArr[3].'</div>';
						$custom_html .= '</div>';
						$custom_html .= '</li>';
					}
				}
			}
			$custom_html .= '</ul>';
			if($attributeList){
				$custom_html2 = '<div class="left" style="margin-right:10px;">';
				$custom_html2 .= '	<dl>';
				$i=1;
				foreach ($attributeList as $attr_id => $attr){
					if($attr_id==71){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
					}elseif($attr_id==70){
						foreach($attributeList[$attr_id]['values'] as $attrv2){
							$i++;
							if(in_array($attrv2['attr_value_id'],$attrArrId)){
								$attrValueName = L('yes');
							}else{
								$attrValueName = L('no');
							}
							$custom_html2 .= '   <dt>';
							$custom_html2 .= '	<span>'.$attrv2['label'].'：</span><span>'.$attrValueName.'</span>';
							$custom_html2 .= '   </dt>';
							if($i==7){
								$custom_html2 .= '</dl> ';
								$custom_html2 .= '</div> ';
								$custom_html2 .= '<div class="middle"></div> ';
								$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
								$custom_html2 .= '<dl> ';
							}
						}

					}elseif($attr_id==74){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
						if($i==7){
							$custom_html2 .= '</dl> ';
							$custom_html2 .= '</div> ';
							$custom_html2 .= '<div class="middle"></div> ';
							$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
							$custom_html2 .= '<dl> ';
						}
					}elseif($attr_id==72 || $attr_id==73){
						$i++;
						if(in_array($attr_id,$attrArrId)){
							$attr_value_id = $attrArrId2[$attr_id];
							$attrValueName = $attrArrName[$attr_value_id];
						}else{
							$attrValueName = L('no');
						}
						$custom_html2 .= '   <dt>';
						$custom_html2 .= '	<span>'.$attr['name'].'：</span><span>'.$attrValueName.'</span>';
						$custom_html2 .= '   </dt>';
						if($i==7){
							$custom_html2 .= '</dl> ';
							$custom_html2 .= '</div> ';
							$custom_html2 .= '<div class="middle"></div> ';
							$custom_html2 .= '<div class="left" style="margin-right:10px;"> ';
							$custom_html2 .= '<dl> ';
						}
					}
				}
				$custom_html2 .= ' </dl>';
				$custom_html2 .= ' </div>';
			}
		}elseif($productInfo['model_id'] == 14){
			$custom_html = '<ul>';
			foreach ($custom as $pattrArr){
				$attrArr = explode(':', $pattrArr);
				$attribute_items_db = D('attribute_items');
				$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $attrArr[0],'language_id' => $language_id))->find();
				$attr_name = $attribute_items_info['name'];

				$attrArrId[] = $attrArr[0];
				$attrArrId2[$attrArr[0]] = $attrArr[4];
				$attrArrId2[] = $attrArr[0];
				$attrArrName[$attrArr[4]] = $attrArr[3];
				if($attrArr[0] != '80' || $attrArr[0] != '81' || $attrArr[0] != '82' || $attrArr[0] != '83' ){
					if($attrArr[0]==80 || $attrArr[0]==81 || $attrArr[0]==82 || $attrArr[0]==83){continue;}
					if($attrArr[3]){
						$attribute_values_db = D('attribute_values');
						$attribute_values_db->field('attr_img');
						$attribute_values_info = $attribute_values_db->find($attrArr[4]);
						$custom_html .= '<li>';
						$custom_html .= '<div class="name">'.$attr_name.':</div>';
						$custom_html .= '<div><img src="'.$attribute_values_info['attr_img'].'" width="110">';
						$custom_html .= '<div class="result">'.$attrArr[3].'</div>';
						$custom_html .= '</div>';
						$custom_html .= '</li>';
					}
				}
			}
			$custom_html .= '</ul>';
		}
		$this->assign('custom_html', $custom_html);
		$this->assign('custom_html2', $custom_html2);
		$this->display('product_conclusion');
	}

    //产品评论
	public function reviews(){
		global $language_id;
		$pid  = intval($_REQUEST['pid']);

        $where = array(
        	'product_id' => $pid,
            'language_id' => $language_id,
            'disabled' => 1,
        );
        $order = "id desc";

        $pageRows = 3;
		$comment_db = D('comment');
		$count = $comment_db->where($where)->count();
		import("ORG.Util.PageListSimple");
		$Page = new Page($count,$pageRows);
		$pageShow = $Page->show();
		$commentList = $comment_db->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

        $comment_photo_db = M('comment_photo');
        //获取用户信息
        foreach ($commentList as $key => $val) {
        	if($val['user_id']>0){
        	    $userInfo = UserModel::get_user_info($val['user_id']);
        	    $email = $userInfo['email'];
        	}else{
        		$email = $val['email'];
        	}

            if($email){
                $emailArr = explode('@', $email);
                $strlen = strlen($emailArr[0]);;
                if($strlen>3){
                   $emailstr = substr($emailArr[0] , 0 ,-3).'***@'.$emailArr[1];
                }else{
                   $emailstr = substr($emailArr[0] , 0 ,-1).'*&'.$emailArr[1];
                }
                $commentList[$key]['email'] = $emailstr;
            }
			$commentList[$key]['content'] = htmlspecialchars($val['content']);
            $commentList[$key]['photo'] = $comment_photo_db->field('img,thumb')->
            where(array('comid'=>$val['id']))->order('fid asc')->select();
        }
        $pageShow = preg_replace("/\/_\/(\d+)'/is", "'", $pageShow);
        $pageShow = preg_replace("/href='\/product\/reviews\/pid\/".$pid."\/p\/(\d+)'/is", "href='javascript:;' onclick='customerReviews($1);'", $pageShow);

		$this->assign('pageShow', $pageShow);
		$this->assign('commentList',$commentList);

		$this->display('product_reviews');
	}

	public function sizeGuide()
	{
		$pid = $this->_request('pid');
		$productInfo = ProductModel::get_product_info($pid);
		$m_data = C('m_data');

		//默认开启
		$sizeType = array(1,2,3,4,5,11);
		if(isset($m_data[$productInfo['model_id']])){
            $sizeType = $m_data[$productInfo['model_id']];
		}
		
		$this->assign('sizeType',$sizeType);
		$this->display('size_guide');
	}
    
    //衬衫清库存活动分享
	public function shareGetEXTRA(){
    	global $user,$language_id;
        if(!UserModel::logged()){
	        $info = array(
	        	'error' => 1,
	        	'errStr' => L('Please login to your OWNONLY account'),
	        );
		    echo json_encode($info);exit;
        }
        //分享的类型
        $sharetype = array('facebook','twitter');
        //分享类型的判断
        $shareName = $_REQUEST['shareName'];
        if(!in_array($shareName, $sharetype)){
	        $info = array(
	        	'error' => 2,
	        	'errStr' => 'Share the way errors',
	        );
		    echo json_encode($info);exit;
        }

        $share_result_db = D('share_result');
        $where = array(
            'user_id' => $user['id'],
            'share'   => $shareName,
            );
        $id = $share_result_db->where($where)->getField('id');
        if(!$id){
            $add_data = array(
                'user_id' => $user['id'],
                'share'   => $shareName,
            );
            $share_result_db->add($add_data);
            //用户用户分享送$5
		    UserModel::user_account_change($user['id'], 5, 0, 1, NULL, 'share', NULL);
			UserModel::log_account_change($user['id'], 5, 0, 0 ,0, 'share', UserModel::ACT_SAVING);
        }

        $info = array(
        	'error' => 0,
        	'errStr' => '',
        );
	    echo json_encode($info);exit;
    }

    //到货通知
    public function notifyMe(){
        global $user,$language_id;
        $pid = intval($_REQUEST['pid']);
        $notifyme = 0;
        if(isset($user['id']) && $user['id']>0 && $pid){
            $notifyme_db = D('notifyme');           
            $where = array(
                'user_id' => $user['id'],
                'pid'     => $pid,
                'is_notify' => 0,
            	);
            $isExist = $notifyme_db->where($where)->count();
            if(!$isExist){
	            $data = array(
	                'user_id' => $user['id'],
	                'pid'     => $pid,
	                'is_notify' => 0,
	                'time'      => time(),
	            	);
	            $notifyme = $notifyme_db->add($data);
            }
        }
        echo intval($notifyme);
    }

    //获取产品数据 ajax
    public function product_ajax_res(){
        $pid = intval($_REQUEST['pid']);
        $product = ProductModel::get_product_info($pid);
        //计算价格
		if($product['attribute_type']==2){
			if($product['product_type']){
			  $price = currency($product['sell_price']);
			}else{
			  $price = currency($product['advanced_price']);
			}
		} elseif ($product['attribute_type']==1){
		    $price = currency($product['custom_price']);
		}        
        $jsonData = array(
            'pid'      => $product['id'],
            'title'    => $product['name'],
            'display'  => $product['display'],
            'list_img' => $product['goods_img'] ? $product['goods_img'] : $product['list_img'],
            'ml_img'   => $product['ml_img'],
            'price'    => $price,
            'summary'  => $product['summary'],
            'href'      => $product['url'],
        	);
        echo json_encode($jsonData);
    }
    //3d搭配试衣间全屏显示的3d搭配商品
    public function product_list_ajax()
    {
        global $language_id;
        $productdb = D('product');
        $data['status']=true;
        $catid = isset($_REQUEST['catid'])?intval($_REQUEST['catid']): null;
        $where ='is_sell=1 and display>=1 and is_delete=0';
        $order= ' new_product desc, sorts  ASC, advanced_price  ASC, catid  ASC,id desc';//默认进入商品列表 价格从低到高排序
        if($catid){
            $arr_category[]=$catid;
            //获取所有子类
            $category_list_son = CategoryModel::get_category_sub_list($catid);
            if( $category_list_son ){
                foreach( $category_list_son as $v){
                    $arr_category[]= $v['id'];
                }
                $str_category=implode(",",$arr_category);
                $where .=" and catid in(".$str_category.")";
            }else{
                $where .=" and catid =".$catid;
            }

        }
        if(!$catid){
            $data['status']=false;
            $data['msg']="缺少分类id";
            echo json_encode($data,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);exit;
        }
        if(cache("product_list_ajax" . $catid )){
            $data['data']=cache("product_list_ajax" .$catid);
        }else {
            $productList = $productdb->field('id')->where($where)->order($order)->select();//echo $productdb->getLastSql();echo "<br>";exit;
            if ($productList) {
                $j=0;
                foreach ($productList as $k => $product) {
                    $productList[$k] = ProductModel::get_product_info($product['id']);
                    //获取最终价格
                    $productList[$k]['price_order'] = ProductModel::get_final_price($productList[$k]['id']);//用于排序
                    $advanced_price = $productList[$k]['advanced_price'];
                    $original_price = $productList[$k]['original_price'];
                    $discounts = round((($original_price - $advanced_price) / $original_price) * 100);
                    $productList[$k]['advanced_price'] = currency(ProductModel::get_final_price($productList[$k]['id']));//用于前端显示
                    //父类id
                    $parent_id=CategoryModel::get_category_parentid($productList[$k]['catid']);
                    if( $parent_id!=2174&&$productList[$k]['catid']!=2174){
                        $productList[$k]['link'] = U('/product/' . $product['id'] . '-' . generateUrl_un($productList[$k]['name']));
                        
                    }else{
                        unset($productList[$k]['advanced_price']);
                    }
                    if ($productList[$k]['display'] == 2) {  //售罄
                        $productList[$k]["mark_num"] = 1;
                        $productList[$k]["mark"] = L('sold__out');
                    } elseif ($productList[$k]['original_price'] != 0) {
                        $productList[$k]["mark_num"] = 3;
                        $productList[$k]["mark"] = $discounts . "%<br>off";
                    } elseif ($productList[$k]['hot'] == 1) { //热销
                        $productList[$k]["mark_num"] = 4;
                        $productList[$k]["mark"] = L('_hot_');
                    } elseif ($productList[$k]['new_product'] == 1) {  //新品
                        $productList[$k]["mark_num"] = 5;
                        $productList[$k]["mark"] = L('_new_1');
                    } else {
                        $productList[$k]["mark_num"] = 0;
                        $productList[$k]["mark"] = "";
                    }
                    //划掉的价格
                    if ($productList[$k]['original_price'] > 0) {
                        $three_price = $productList[$k]['original_price'];
                        $productList[$k]['price'] = $three_price;
                    }
                    if ($productList[$k]['price']) {
                        $productList[$k]['price'] = currency($productList[$k]['price']);
                    } else {
                        $productList[$k]['price'] = 0;
                    }
                    if($productList[$k]['3d_show2']==false){
                        unset($productList[$k]);
                    }else{
                        $j++;
                    }
                    if($productList[$k]) {
                        $product_list[$j-1] = $productList[$k];
                    }
                }
            }
            unset($productList);
            if($product_list) {
                $data['data'] = $product_list;
            }else{
                $data['status']=false;
                $data['msg']="没有相关产品";
            }
            cache("product_list_ajax" . $catid, $product_list, 30 * 60);
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    }
    //列表页筛选
    function ajax_product_list(){
        global $language_id;
        $productdb = D('product');
        $cat_ids = $_REQUEST['cat_ids']?intval($_REQUEST['cat_ids']):0;
        $product_colour_id=$_REQUEST['product_colour_id']?$_REQUEST['product_colour_id']:0;
        $colour_id=$_REQUEST['colour_id']?$_REQUEST['colour_id']:0;
        $advanced_price=$_REQUEST['advanced_price']?$_REQUEST['advanced_price']:0;
        $new_product=$_REQUEST['new_product']?intval($_REQUEST['new_product']):0;
        $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : null;
        $where ='is_sell=1 and display>=1 and is_delete=0';

        if($keyword){
            $searchKeyword = str_replace('color', '', $keyword);
            $searchKeyword = strtolower($searchKeyword);
            if(preg_match('/blazers|blazer/', $searchKeyword))
                $searchKeyword .= ' blazer';
            if(preg_match('/suit|suits/', $searchKeyword))
                $searchKeyword .= ' suit';
            if(preg_match('/men|mens/', $searchKeyword))
                $searchKeyword .= ' men';
            if(preg_match('/Striped|chambray/', $searchKeyword))
                $searchKeyword .= ' chambray Striped';
            if(preg_match('/shirt|shirts/', $searchKeyword))
                $searchKeyword .= ' shirt';
            if(preg_match('/tuxedo|wedding/', $searchKeyword))
                $searchKeyword .= ' dress';
            if(preg_match('/2 piece/', $searchKeyword))
                $searchKeyword .= ' two piece';
            if(preg_match('/3 piece/', $searchKeyword))
                $searchKeyword .= ' three piece';
            if(preg_match('/2 button/', $searchKeyword))
                $searchKeyword .= ' two button';
            if(preg_match('/3 button/', $searchKeyword))
                $searchKeyword .= ' three button';
            if(intval($keyword)){
                $where.= ' and product.id='.intval($keyword);
            }else{
                /* //用空格分割关键词
                $searchKeyword = $keyword;
                $searchKeyword = explode(' ', $searchKeyword);
                $where_search_str = '';
                foreach ($searchKeyword as $searchVal) {
                    if($searchVal){ */
                $where.= ' and product_items.name LIKE "%' . $keyword . '%"';
                //	}
                //}
                /* if($where_search_str){
                    $where_search_str = trim($where_search_str, 'or');
                    $where['_string'] = $where_search_str;
                } */
            }
            $where.= ' and product_items.language_id='.$language_id;
            $productdb->join('product_items ON product_items.product_id = product.id');
        }


        if( $cat_ids ){
            $arr_category[]=$cat_ids;
            //获取所有子类
            $category_list_son = CategoryModel::get_category_sub_list($cat_ids);
            foreach( $category_list_son as $v){
                $arr_category[]= $v['id'];
            }
            $str_category=implode(",",$arr_category);
            $where .=" and catid in(".$str_category.")";
        }
        if($product_colour_id){
            $product_colour_id=explode(",",$product_colour_id);
            $arr_size=sizeof($product_colour_id);
            if($arr_size==1){
                $where.=" and  FIND_IN_SET(".$product_colour_id[0].",product_colour_id)";
            }else if($arr_size>1){
                $where.=" and ( ";
                foreach ($product_colour_id as $k=>$v){
                    $where.=" FIND_IN_SET(".$v.",product_colour_id) ";
                    if($k<$arr_size-1){
                        $where.=" or ";
                    }
                }
                $where.=" ) ";
            }
        }
        if($colour_id){
            $colour_id=explode(",",$colour_id);
            $size=sizeof($colour_id);
            if($size==1){
                $where.=" and  FIND_IN_SET(".$colour_id[0].",colour_id)";
            }else if($size>1){
                $where.=" and ( ";
                foreach ($colour_id as $k=>$v){
                    $where.=" FIND_IN_SET(".$v.",colour_id)";
                    if($k<$size-1){
                        $where.=" or ";
                    }
                }
                $where.=" ) ";
            }
        }
        if($new_product&&!$keyword){
            $where.=" and new_product=1";
        }
        if(!$new_product){

            if($advanced_price==1){
                $order= ' advanced_price  ASC,new_product desc,sorts  ASC,id desc';//默认进入商品列表 价格从低到高排序
            }else if($advanced_price==2){
                $order= ' advanced_price  desc,new_product desc,sorts  ASC,id desc';//默认进入商品列表 价格从低到高排序
            }else{
                $order= ' new_product desc, sorts  ASC,id desc';//默认进入商品列表 价格从低到高排序
            }

        }

        if($advanced_price==1){
            if($new_product){
                $order="   advanced_price ASC,new_product desc,sorts asc,id desc";
            }
        }elseif($advanced_price==2){
            if($new_product){
                $order="   advanced_price desc,new_product desc,sorts asc,id desc";
            }
        }else{
            if($new_product){
                $order="new_product desc, sorts asc,id desc";
            }
        }
        if(cache("product_list_ajax" . $cat_ids . $new_product.$advanced_price.$colour_id.$product_colour_id)){
            $respon['status']=true;
            $respon['data']=cache("product_list_ajax" . $cat_ids . $new_product.$advanced_price.$colour_id.$product_colour_id);
            echo json_encode($respon,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);exit;
        }else {
            $productList = $productdb->field('id')->where($where)->order($order)->select(); //echo $productdb->getLastSql();echo "<br>";exit;
            if ($productList) {
                foreach ($productList as $k => $product) {
                    $productList[$k] = ProductModel::get_product_info($product['id']);
                    //获取最终价格
                    $productList[$k]['price_order']=ProductModel::get_final_price($productList[$k]['id']);//用于排序
                    $advanced_price = $productList[$k]['advanced_price'];
                    $original_price = $productList[$k]['original_price'];
                    $discounts = round((($original_price - $advanced_price) / $original_price) * 100);
                    $productList[$k]['advanced_price']= currency(ProductModel::get_final_price($productList[$k]['id']));//用于前端显示
                    $productList[$k]['link'] = U('/product/' . $product['id'] . '-' . generateUrl_un($productList[$k]['name']));

                    if ($productList[$k]['display'] == 2) {  //售罄
                        $productList[$k]["mark_num"] = 1;
                        $productList[$k]["mark"] = L('sold__out');
                    }
                  /*  elseif ($productList[$k]['is_yushou'] == 1) {//预售
                        $productList[$k]["mark_num"] = 2;
                        $productList[$k]["mark"] = L('_yushsou_');
                    }*/
                    elseif ($productList[$k]['original_price'] != 0) {
                        $productList[$k]["mark_num"] = 3;
                        $productList[$k]["mark"] = $discounts."%<br>off";
                    }
                    elseif ($productList[$k]['hot'] == 1) { //热销
                        $productList[$k]["mark_num"] = 4;
                        $productList[$k]["mark"] =  L('_hot_');
                    }
                    elseif ($productList[$k]['new_product'] == 1) {  //新品
                        $productList[$k]["mark_num"] = 5;
                        $productList[$k]["mark"] =  L('_new_1');
                    }
                    else {
                        $productList[$k]["mark_num"] = 0;
                        $productList[$k]["mark"] = "";
                    }
                    //划掉的价格
                    if($productList[$k]['is_yushou']==1){
                        if ($productList[$k]['original_price'] > 0) {
                            $three_price = $productList[$k]['original_price'];
                            $productList[$k]['price'] =  $three_price;
                        } else{
                            if($productList[$k]['attribute_type']==2){
                                if($productList[$k]['product_type']){
                                    $three_price = $productList[$k]['sell_price'];
                                    $productList[$k]['price'] =$three_price;
                                }else{
                                    $three_price = $productList[$k]['advanced_price'];
                                    $productList[$k]['price'] =  $three_price;
                                }
                            } elseif ($productList[$k]['attribute_type']==1){
                                $three_price = $productList[$k]['custom_price'];
                                $productList[$k]['price'] = $three_price;
                            }
                        }
                    }else if($productList[$k]['is_yushou']==0) {
                        if ($productList[$k]['original_price'] > 0) {
                            $three_price = $productList[$k]['original_price'];
                            $productList[$k]['price'] = $three_price;
                        }
                    }
                    if($productList[$k]['price']){
                        $productList[$k]['price']=currency($productList[$k]['price']);
                    }else{
                        $productList[$k]['price']=0;
                    }
                }
                cache("product_list_ajax" . $cat_ids . $new_product.$advanced_price.$colour_id.$product_colour_id, $productList, 30 * 60);
                $respon['status'] = true;
                $respon['data'] = $productList;
                echo json_encode($respon, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
                exit;
            } else {
                echo json_encode(array("status" => false, "msg" =>'没有相关产品'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
            }
        }
    }

	public function show(){
		global $user,$language_id;
		$type = I("get.type","","intval");
		$cid  = I("get.cid","","intval");
		$productInfo = ProductModel::get_product_info($type);
		if(!$productInfo){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}

		if($productInfo['attribute_type'] != 2){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));
		}
		
		if($productInfo['display'] == 0){
			$this->error(L('_PRODUCT_NO_EXIST_'), url(''));	
		}
		
		$mutex_arr = array(
			'274'=>array(
				'1587'=> array(
					'280'=>array(1599)
				),
				'1586'=> array(
					'280'=>array(1599)
				),
				'1588'=> array(
					'280'=>array(1599)
				)
			),
			'280'=>array(
				'1599'=> array(
					'274'=>array(1586,1587)
				),
				'1598'=> array(
					'274'=>array(1588)
				)
			)
		);

		$data['productInfo'] = $productInfo;
		
		$attributeFile = C("3d_product_attribute_file_url");

		$attrData = '';
		if($cid > 0){
			$cart_products_db = D('cart_products');
		
			$cart_product = $cart_products_db->field('add_time, attr, measure_id')->where(array('id' => $cid))->find();
			if($cart_product){
				$attrData = $cart_product['attr'];
		    }
		}
		list($attributeList, $size_type, $attrOptionArr) = CartModel::cart_attr_show($type, $attrData, true);

		$is_show_attributeList = array();
		$no_show_attributeList = array();
		$monogram_attributeList = array();
		foreach($attributeList as $attr_k=>$attr_v){
			if($attr_v['is_show_sd']==1){
				$is_show_attributeList[$attr_k] = $attr_v;
			}else{

				if($attr_v['is_monogram']==1){
					if(strstr(strtolower($attr_v['name']),"color")){ //绣字颜色
						$monogram_attributeList["color"] = $attr_v;
					}else if(strstr(strtolower($attr_v['name']),"font")){ //绣字字体
						$monogram_attributeList["font"] = $attr_v;
					}else if(strstr(strtolower($attr_v['name']),"location")){ //绣字位置
						$monogram_attributeList["location"] = $attr_v;
					}else if(strstr(strtolower($attr_v['name']),"monogram")){
						$monogram_attributeList["monogram"][] = $attr_v;
					}
				}else{
					$no_show_attributeList[$attr_k] = $attr_v;
				}
			}
		}
		$attr_arr=$is_show_attributeList;
		foreach($attr_arr as $k=>$v){
			if($v['is_ll']!=0){
				unset($attr_arr[$k]);
			}

		}
		$data['attr_arr'] = $attr_arr;
		switch($type){
			case 1994:
  				$productFileName="SHIRTS";
				$Fabrics=$attributeList[253]['values'][0]['desc_name'];
  				break;  
			case 1995:
 				$productFileName="SUITS";
				$Fabrics=$attributeList[259]['values'][0]['desc_name'];
  				break; 
			case 1996:
 				$productFileName="PANTS";
				$Fabrics=$attributeList[269]['values'][0]['desc_name'];
  				break;
			default:
				$productFileName="SHIRTS";
		}
		//更新产品访问量
		ProductModel::update_product_visit($type);
		//图库
		$data['photoList'] = ProductModel::get_product_photo_list($type);
		$data['fabrics']   = $Fabrics;
		$data['fabrics_name']   = strtolower($productFileName);
		$data['quickbuy']   = 0;
		$data['url'] 				= "/data/3d/".$productFileName;
		$data['productFileName'] 	= $productFileName;
		$data['sel_custom_val']		= $user['sid'];
		$data['attributeList'] 		= $attributeList;
		$data['is_show_attributeList'] 		= $is_show_attributeList;
		$data['no_show_attributeList'] 		= $no_show_attributeList;
		$data['monogram_attributeList'] 		= $monogram_attributeList;
		$data['web_url'] 			= "http://".$_SERVER['HTTP_HOST'];//"http://www.metiriclubnew.com";
		$data['mutex_arr'] = $mutex_arr;

		$this->assign($data);
		$this->display('show');	
	}
	
	public function listDir($dir){
		$array=array();
		if(is_dir($dir)){
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false){
					if($file!="." && $file!=".."){
						array_push($array,$file);
					}
				}
				closedir($dh);
			}
		}
		return $array;
	}
	
}